about initializing

gorelshv

Member
Licensed User
Longtime User
Hi,
how can I know when is it safe to initialize an object in code multiple times without causing a memory leak (for example to read battery charge)?
Also, it seems that it is possibe to initialize an object in sub_globals as well as in any other place in the code. Is there any difference (for better or worse) between the two methods?
I'm a long time user of basic4android but I've never seem to get a clear view on these matters...:eek:
 

gorelshv

Member
Licensed User
Longtime User
Hi Erel, thanks for the replay.
Why is calling initialize in Activity_Create is better then calling it in Sub_Globals, aside from making the code more tidy?
"Calling initialize multiple times will not cause a memory leak" - does this apply to all objects?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Sub globals is only intended for variables declaration. In most cases your initialization code will work. However as this is not the intended usage there maybe unexpected issues.

"Calling initialize multiple times will not cause a memory leak" - does this apply to all objects?
Any object that is not referenced will be eventually released by the garbage collector.

There may be other side effects. For example if an object opens a file during initialization and this file is not close, then there will be a file handle leak.
 
Upvote 0
Top