Menu

Cfg Alloc/Dealloc for jni

2015-09-18
2016-03-22
  • William Smith

    William Smith - 2015-09-18

    There are a number of methods used by JNI for managing arrays. GetPrimitiveArrayCritical, NewByteArray, etc. When trying to create a jni.cfg, I can seem to figure out how to enter them in the cfg xml so that they are recognized. example:

    <?xml version="1.0"?>
    <def>
      <resource>
        <alloc>GetPrimitiveArrayCritical</alloc>
        <dealloc>ReleasePrimitiveArrayCritical</dealloc>
      </resource>
    </def>
    

    When I use this config and the source makes references like:

    ubyte *ivData = (ubyte *)env->GetPrimitiveArrayCritical(ivArray, 0);
    

    cppcheck doesn't identify the line unless I remove the env-> reference. Is there a way to put this in a cfg?

    TIA,
    Bill

     
    • orbitcowboy

      orbitcowboy - 2015-09-18

      You have to tweak your configuration (see attachment jni.cfg).

      Then, the following test case:

      void foo()
      {
      ubyte ivData = (ubyte )GetPrimitiveArrayCritical(10, 0);
      }

      gives

      $ cppcheck --library=jni jni_test.cpp
      Checking jni_test.cpp...
      [jni_test.cpp:4]: (error) Memory leak: ivData

       

      Last edit: orbitcowboy 2015-09-18
  • Daniel Marjamäki

    You need to specify the scope where the function is declared.

    Configuration:

    <?xml version="1.0"?>
    <def format="2">
      <memory>
        <alloc>Env::GetPrimitiveArrayCritical</alloc>
        <dealloc>Env::ReleasePrimitiveArrayCritical</dealloc>
      </memory>
    </def>
    

    Testcode:

    typedef unsigned char ubyte;
    
    void foo(Env *env) {
      ubyte *ivData = (ubyte *)env->GetPrimitiveArrayCritical(ivArray, 0);
    }
    

    Output from Cppcheck:

    daniel@debian:~/cppcheck$ ./cppcheck --library=jni jnitest.cpp 
    Checking jnitest.cpp...
    [jnitest.cpp:6]: (error) Memory leak: ivData
    
     
    • William Reich

      William Reich - 2016-03-22

      I have a similar issue.
      I have many classes in C++ that have getReadLock() and getWriteLock() and unLock().
      I would like to use a 'resource' token in the cfg file to try and catch
      the act of not releasing a lock.

      When I have a cfg file like this:
      <?xml version="1.0"?>
      <def>
      <resource>
      <alloc>getReadLock</alloc>
      <dealloc>releaseLock</dealloc>
      <dealloc>unLock</dealloc>
      </resource>
      <resource>
      <alloc>getWriteLock</alloc>
      <dealloc>releaseLock</dealloc>
      <dealloc>unLock</dealloc>
      </resource>
      </def>

      all of the 'standalone' items are recognized, ( getReadLock() ; ),
      but the another types of references are not
      ptr->getReadLock() ;

      Also I have these lock functions in many many classes. Is there a way to write the cfg file to catch all classes of getReadLock(), etc ?

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.