B4A Library [Lib] StrictMode

Discussion in 'Additional libraries, classes and official updates' started by Informatix, Jun 11, 2013.

  1. Informatix

    Informatix Expert Licensed User

    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):
    Code:
    Dim SM As StrictMode
    SM.EnableStrictModeForAll(
    TrueFalse)
    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.
     

    Attached Files:

    Last edited: Jun 11, 2013
  2. Informatix

    Informatix Expert Licensed User

    An example of memory leak (in the SoundPool library):
     
  3. Informatix

    Informatix Expert Licensed User

    An example of SQLite cursor that was not closed:
     
  4. joneden

    joneden Active Member Licensed User

    Looks great! I'll have to have a look at this tomorrow.
     
  5. Helihead

    Helihead Member Licensed 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
     
  6. Informatix

    Informatix Expert Licensed User

    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.

    The StrictMode API has been written by Google engineers. It is supposed to work flawlessly.
     
    Last edited: Jun 24, 2013
  7. amidgeha

    amidgeha Member Licensed User

    Is it legal to disable strict mode on google play?
     
  8. Informatix

    Informatix Expert Licensed User

    It's allowed, of course.
     
    amidgeha likes this.
  9. CanguroCode

    CanguroCode Active Member Licensed User

    Hi, is there any similar library for B4J?
     
    Last edited: Apr 21, 2017
  10. Informatix

    Informatix Expert Licensed User

    No.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice