tags:

views:

629

answers:

5

I thought this question would have been asked before, but I couldn't find it here...

I've used SWIG to create a JNI wrapper around a C++ class. All works great except that Java never seems to call the class's finalize(), so, in turn, my class's destructor never gets called. The class's destructor does some final file I/O, so unfortunately, this isn't just a minor memory leak.

Searching through Google, there doesn't seem to be a way to force Java to GC and destroy an object. True?

I know I could manipulate my SWIG file and create a java function that would call the C++ destructor, but this class is used by end users in several different platforms/languages, so the addition of a Java-only will create an inconsistency that our tech writers aren't going to like.

+5  A: 

Java finalizers are mostly useless, in my opinion, and certainly not a replacement for C++ destructors. Unfortunately, Java has no replacement for C++ RAII.

Don't bother trying to force Java finalization. When you're through with the whatever, all a function that will dispose of it. That's all you can do.

David Thornley
+5  A: 

You can't force GC with System.gc(). Also it is not guaranteed that there will ever be a GC run for example if your app runs only for a short time and then your finalizer won't run at all (the JVM does not run it when exiting). You should create a close() or destroy() or whatever function for your class and when you finished using an instance of this class call it, preferably from a finally block, like.


MyClass x = null;
try{
    x = new MyClass();
    x.work();
} finally {
    if (x!=null)
        x.close();
}
Gábor Hargitai
A: 

Issues like this are the reason C# chose the IDisposable pattern for deterministic finalization.

I suggest you follow the same pattern and adapt it for your Java users.

In your c++ class, create a separate, public method that disposes of your resources. Call it close, or dispose, or something.

Have your C++ destructor call the public method, and tell managed/GC users of the C++ class that they must call the method to avoid memory leaks.

Randolpho
+3  A: 

If you're relying on code in the finalize method to run at a certain time, you need to reconsider your approach. The problem here is that you don't know when finalize will be called by the JVM, since you don't know when the object will be garbage collected.

One thing that you should consider, since your class will be re-used in other projects, is that it's possible that an end-user could use an instance of a class in such a way that it will not be collected or that garbage collection will be unlikely, such as creating a static reference to an instance of the class. I think creating a close or destroy method is your safest bet to ensure that the resources the instance of the C++ class associated with your Java object is using are released appropriately.

Since re-use is a concern, you could have the C++ destructor check to see if the resources had been released and to call the same code if they hadn't been, like so:

class MyThing {
  public:
    void close();
    ~MyThing();

  private:
    bool released = false;
};

void
MyThing::close() {
  // close logic here
  this->released = true;
}

MyThing::~MyThing() {
  if (!released) {
    this->close();
  }
}

This way, your existing C++ code hopefully won't have to change much, and you can ensure that your resources are released in a deterministic way in the context of native code running via JNI.

Paul Morie
A: 

After some more looking through the SWIG-produced code, I see that the SWIG people actually have already dealt with this -- they add a delete() function for you. It looks to pretty well taken care of the possibility of both the programmer and the GC deleting the object as well.

Marc Bernier