I have a large body of C++ code that I've wrapped with SWIG and am calling it from Java. The C++ code makes liberal use of boost smart pointers.
Some of my JUnit tests complete but then experience seg faults during cleanup. The stack trace indicates a memory error in an object's finalization, but it's happening in the JNI code generated by SWIG and seems to be associated with the smart pointer reference counting.
I would like to be able to step through all layers of the code. Is this possible? I would also be very happy to hear others' experiences with this sort of problem.
You can attach a second, native-code debugger to the executing Java code. For example, with Visual Studio, you can attach via "Debug>Attach to process."
SWIG supports smart pointers, but you have to manage their lifetime explicitly on the Java side. If you obtain ownership of a smart-pointer object on the Java side, you must delete it. Bear in mind that Java has no object temporaries that go out of scope, so you won't be able to use expressions like f().g().h(), in which the return values at each level are smart pointers.
As a failsafe, the SWIG-generated finalizer will attempt to delete it for you if garbage collection occurs. That failsafe will be harmful if the object has already been deleted on the C++ side.