I have a Java program which calls into my C++ library. JNI-C++ bridging is created using SWIG
I am relatively new to Java & trying to understand the behavior of System.gc & System.runFinalization
This code below causes a SIGABRT, killing my app (2 out of 10 runs)
public static void main (String[] args)
{
try
{
doSomeStuff();
} catch(Exception ex)
{
System.out.println(ex.getMessage());
} finally {
System.runFinalization();
System.gc();
}
}
void doSomeStuff()
{
try
{
sampleCppClass cppClass = new sampleCppClass();
/*
* do some stuff with Cpp obj
*/
} catch (Exception ex)
{
System.out.println(ex.getMessage());
} finally {
System.runFinalization();
System.gc();
}
}
Whereas this one works fine
public static void main (String[] args)
{
try
{
doSomeStuff();
} catch(Exception ex)
{
System.out.println(ex.getMessage());
} finally {
System.runFinalization();
System.gc();
}
}
void doSomeStuff()
{
try
{
sampleCppClass cppClass = new sampleCppClass();
/*
* do some stuff with Cpp obj
*/
} catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
I know that System.gc() & System.runFinalization() are non-deterministic(Source-1, Source-2 and Source-3).
Can it so happen that the System.gc() in doSomeStuff gets executed & free's the obj.
Later on, the System.gc() in main runs & tries to free the same obj causing a crash? If this is indeed the case, is there a way to prove it (logs/prints to show the order of object free'ed by gc)?
This SO post, explains causes of SIGABRT
Since I am running in release mode, my C++ lib doesnt have any assert/abort. Hence I am concerned about double/multiple free's of the object in consideration.