B4A Library [Lib] StrictMode

This library enables the StrictMode. It's useful to detect memory leaks, SQL cursors still opened or undesirable accesses to disk.
The library includes also two functions returning the available memory of your application and the remaining memory.

Example of use (right after Activity_Create):
B4X:
Dim SM As StrictMode
SM.EnableStrictModeForAll(True, False)
The report is made in the system log. Uncheck Filter if you want to see the log entries in the B4A log window. IMHO, you should better use LogCat of the Monitor tool (a tool provided with the Android SDK). LogCat allows custom filters (you can filter the tag "StrictMode" or your application name).

Please don't ask me questions on how to debug your application with StrictMode. I provide a library, not a debugging service. ;)

Don't forget to remove the calls to this library when you release your product.
 

Attachments

  • StrictMode v1.0.zip
    3.1 KB · Views: 575
Last edited:

Informatix

Expert
Licensed User
Longtime User
An example of memory leak (in the SoundPool library):
A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:184)
at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:412)
at android.content.res.AssetManager.openAssetFd(Native Method)
at android.content.res.AssetManager.openFd(AssetManager.java:331)
at anywheresoftware.b4a.audio.SoundPoolWrapper.Load(SoundPoolWrapper.java:33)
at flm.b4a.playground.codcommun._vv7(codcommun.java:169)
 

Informatix

Expert
Licensed User
Longtime User
An example of SQLite cursor that was not closed:
Finalizing a Cursor that has not been deactivated or closed. database = /data/data/flm.b4a.playground/files/Preferences, table = null, query = SELECT value FROM main WHERE key = ?
android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
at android.database.sqlite.SQLiteCursor.<init>(SQLiteCursor.java:98)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:50)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
at anywheresoftware.b4a.sql.SQL.ExecQuery2(SQL.java:167)
at flm.b4a.playground.clskeyvaluestore._vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv0(clskeyvaluestore.java:145)
 

Helihead

Member
Licensed User
Longtime User
Just tried it out but may have an issue

Hi,

I added the library and the code
"Dim SM As StrictMode
SM.EnableStrictModeForAll(True, False)"
In the activity create.

When I load my program on to the phone I see the following in the LOg:

"Could not find method android.os.StrictMode$VmPolicy$Builder.detectActivityLeaks, referenced from method flm.b4a.strictmode.StrictModeForB4A.EnableStrictModeForActivity
VFY: unable to resolve virtual method 829: Landroid/os/StrictMode$VmPolicy$Builder;.detectActivityLeaks ()Landroid/os/StrictMode$VmPolicy$Builder;
VFY: replacing opcode 0x6e at 0x0010
VFY: dead code 0x0013-0018 in Lflm/b4a/strictmode/StrictModeForB4A;.EnableStrictModeForActivity ()V
Could not find method android.os.StrictMode$VmPolicy$Builder.detectLeakedClosableObjects, referenced from method flm.b4a.strictmode.StrictModeForB4A.EnableStrictModeForClosableObjects
VFY: unable to resolve virtual method 831: Landroid/os/StrictMode$VmPolicy$Builder;.detectLeakedClosableObjects ()Landroid/os/StrictMode$VmPolicy$Builder;
VFY: replacing opcode 0x6e at 0x0010
VFY: dead code 0x0013-0018 in Lflm/b4a/strictmode/StrictModeForB4A;.EnableStrictModeForClosableObjects ()V
Could not find method android.os.StrictMode$VmPolicy$Builder.detectLeakedRegistrationObjects, referenced from method flm.b4a.strictmode.StrictModeForB4A.EnableStrictModeForRegistrationObjects
VFY: unable to resolve virtual method 832: Landroid/os/StrictMode$VmPolicy$Builder;.detectLeakedRegistrationObjects ()Landroid/os/StrictMode$VmPolicy$Builder;
VFY: replacing opcode 0x6e at 0x0010
VFY: dead code 0x0013-0018 in Lflm/b4a/strictmode/StrictModeForB4A;.EnableStrictModeForRegistrationObjects ()V
Could not find method android.os.StrictMode$ThreadPolicy$Builder.detectCustomSlowCalls, referenced from method flm.b4a.strictmode.StrictModeForB4A.EnableStrictModeForSlowCalls
VFY: unable to resolve virtual method 819: Landroid/os/StrictMode$ThreadPolicy$Builder;.detectCustomSlowCalls ()Landroid/os/StrictMode$ThreadPolicy$Builder;
VFY: replacing opcode 0x6e at 0x0010
VFY: dead code 0x0013-0018 in Lflm/b4a/strictmode/StrictModeForB4A;.EnableStrictModeForSlowCalls ()V"

I'm not sure I am seeing the correct strictmode output when I run the program but I do see the following:

"Finalizing a Cursor that has not been deactivated or closed. database = /mnt/sdcard/Android/data/RockDat.com/files/rockdat.sal, table = null, query = SELECT * FROM MasterList ORDER BY manufacturer,item ASC
android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here"

I am doing a cursor.close and a sql.close after the code that calls this.

Is the strictmode working correctly?

Why does it say I'm not closing the cursor when I am?

Thanks
Jeff
 

Informatix

Expert
Licensed User
Longtime User
unable to resolve virtual method

This error message just warns you that your Android version is too old to use this detection method. As said in the help of the EnableStrictModeForAll function:
"Before Honeycomb, this is limited to SQLite objects, disk accesses and network operations."
Use the emulator under JellyBean to get access to the whole range of methods.

Is the strictmode working correctly?

The StrictMode API has been written by Google engineers. It is supposed to work flawlessly.
 
Last edited:

CanguroCode

Active Member
Licensed User
Longtime User
Hi, is there any similar library for B4J?
 
Last edited:
Top