Using a %exception directive will wrap all JNI calls automatically. We use something like this to catch OutOfMemory exceptions:
%exception %{
    try {
    } catch (std::bad_alloc &) {
      return $null;
    } catch (std::exception &e) {
      jclass clazz = jenv->FindClass("java/lang/Exception");
      jenv->ThrowNew(clazz, e.what());
      return $null;
    } catch (...) {
      jclass clazz = jenv->FindClass("java/lang/Exception");
      jenv->ThrowNew(clazz, "Unknown exception");
      return $null;

We also overload operator new and delete for all our libraries to use the Java Heap as described here:

The operator new and delete uses the Java heap. When Java runs out of memory it will force those implementations to throw bad_alloc. The bad_alloc can then propagate through C++ correctly to then be caught by the above exception directive. That’s why the bad_alloc catch block simply returns null, because the operator new and delete already set the java exception in the java environment.  

Remember there are pitfalls defining operator new and delete in shared libraries. We do something similar to what is described here: Basically, make operator new and delete local to each shared library, that implementation than calls through to some implementation of a JAVA_malloc and JAVA_free that is located in some other mutual shared library.

Works beautifully for catching OutOfMemory.  

Even with all that we still had cases where we could create objects faster than the JAVA finalizer thread could clean them up. We’re in the middle of implementing something similar to Works well, but we had to make significant modifications. I’ll post more on it once I have time past our release date.  


On 1/28/09 12:03 PM, "David Creasy" <> wrote:


I'm using SWIG in a simple way and it does what we need just perfectly.
Almost.. In very low memory conditions, we get crashes while we'd like
something a 'little more refined.'

The java wrapper produced has for example:

public class myClass {
   private long swigCPtr;
   protected boolean swigCMemOwn;

In the java constructor, it calls the C++ myClass constructor and
assigns it to the swigCPtr. This then obviously gets used in the later
functions, for example:

    public String getValue() {
     return mylibJNI.myClass_getValue(swigCPtr, this);

The problem is that if the C++ constructor fails (out of memory), then
the swigCPtr is null, but this is never checked and it then crashes when
trying to use the null pointer in, for example the jni getValue function.

So, it would be great if it could throw a java exception when the
constructor fails. We've got a very large number of classes, and I don't
want to have to add a %exception or something for each one. I guess in
an ideal world it would be nice if the C++ constructor failed, it could
call System.gc() and then retry the creation of the C++ object.

Apologies if I've missed something obvious.


This email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
Swig-user mailing list