Results 1 to 5 of 5
  1. #1
    ErdyMurphy is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default JNI Memoryproblem when freeing array

    Hello,

    i have a problem with a c++ dll which i use in my program.
    The following code makes problems:

    JNIEXPORT jfloatArray JNICALL Java_dbLib_ReadBinaryFile_getSpectrum
    (JNIEnv *env, jclass jc, jstring file, jlong scan, jint func)
    {
    //COM-Objekt initialisieren
    CoInitialize(NULL);
    IDACSpectrumPtr pSpectrum = IDACSpectrumPtr(CLSID_DACSpectrum);

    //Javastrings parsen
    const char* c_file = env->GetStringUTFChars(file, 0);
    jfloatArray outArray;

    //benötigte Variablen
    VARIANT res;
    long peaks;
    float *intensities, *masses;

    //Anzahl der Datenpunkte bestimmen und Array initialisieren
    peaks = getDataPoints(c_file, func, scan);
    outArray = env->NewFloatArray((int)peaks*2);

    //Daten lesen
    pSpectrum->GetSpectrum(c_file, func, 0, scan);
    pSpectrum->get_Intensities(&res);
    intensities = getData(res);
    pSpectrum->get_Masses(&res);
    masses = getData(res);

    //Daten in ein JavaArray kopieren
    env->SetFloatArrayRegion(outArray, 0, peaks, intensities);
    env->SetFloatArrayRegion(outArray, peaks, peaks, masses);

    //Aufräumen
    env->ReleaseStringUTFChars(file, c_file);
    delete [] intensities;
    delete [] masses;

    return outArray;
    }

    If i remove the last two delete lines:
    delete [] intensities;
    delete [] masses;
    then the program works without problems otherwise i get this error message:
    #
    # A fatal error has been detected by the Java Runtime Environment:
    #
    # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x775a329d, pid=2672, tid=3228
    #
    # JRE version: 6.0_24-b07
    # Java VM: Java HotSpot(TM) Client VM (19.1-b02 mixed mode, sharing windows-x86 )
    # Problematic frame:
    # C [ntdll.dll+0x3329d]
    #
    # If you would like to submit a bug report, please visit:
    # HotSpot Virtual Machine Error Reporting Page
    # The crash happened outside the Java Virtual Machine in native code.
    # See problematic frame for where to report the bug.
    #

    I guess i make a mistake when freeing the arrays...
    But my JNI knowledge is very poor so i dont know what the
    JNI-Functions make in detail.

  2. #2
    ErdyMurphy is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    I found another interesting code snippet in the ddl...
    the getData function:

    float* getData(VARIANT in) {
    float HUGEP *out;
    HRESULT hr = SafeArrayAccessData(
    in.parray,
    (void HUGEP**)&out
    );
    hr = SafeArrayUnaccessData(in.parray);
    return out;
    }

    could anybody give me a hint what this means exactly?
    And if this could raise this accesviolationmessage?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    If those float arrays are allocated and created by the JVM you can't delete[] them in C++. The Java side has to do that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    ErdyMurphy is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    if you look at the code, you see that the array are created in the DLL...

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by ErdyMurphy View Post
    if you look at the code, you see that the array are created in the DLL...
    No, I don't see that; all I see is that the JVM creates those arrays and you're trying to delete[] them in your C++ code. (btw, it would help if you put your code in [code] ... [/code] tags).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 11:32 AM
  2. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  3. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  4. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •