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...
Calling initialize multiple times will not cause a memory leak. If you need to create a new object then you should first Dim the variable and then call Initialize.
Sub Globals should not be used to initialize objects. You should instead do it in Activity_Create.
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?
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.