[vtkusers] Re: is VTKDelete() intended for public use?
Jeff Lee
jeff at cdnorthamerica.com
Fri Oct 7 07:23:45 EDT 2005
Hi Steve,
One way would be to make sure you remove all references to the vtk
object, then call System.gc() which will do the finalize, and actually
delete the pointer immediately. I am not opposed to your patch, but
VTKDelete has reference counting in it, and if someone else has a
reference somewhere, the object won't be deleted anyway. For me,
explicitly calling VTKDelete is philosophically not the right thing to
do. In java, you should make sure the reference tree is clean for the
object, and then it will be finalized when garbage collection occurs.
If the vm is memory bound, then the garbage collection will be invoked
more often, ensuring that the vtkImageData will be deleted shortly after
all references are removed. If you want to make it happen right away,
call System.gc() to manually invoke the garbage collector.
-Jeff
Steve M. Robbins wrote:
>Hello,
>
>This question concerns the java wrapping.
>
>Since VTK objects like vtkImageData are potentially very large, a
>colleague and I were looking for a way to release the resources
>immediately (from java). We found method VTKDelete() in
>vtkObjectBase, which sounds like it fits the bill and is a public
>method. Is this the intended way to release VTK resources?
>
>Unfortunately, VTKDelete() is not usable because it can only be called
>once and the finalizer is -- correctly -- calling it. See the java
>code at the end for a demonstration. The trouble is that the JNI code
>is not handling a null return from vtkJavaGetPointerFromObject().
>
>The current code is:
>
>extern "C" JNIEXPORT void JNICALL Java_vtk_vtkObjectBase_VTKDelete(JNIEnv *env,jobject obj)
>{
> vtkObjectBase *op;
> op = (vtkObjectBase *)vtkJavaGetPointerFromObject(env,obj,(char *) "vtkObjectBase");
> vtkJavaDeleteObject(env,obj);
> op->Delete();
>}
>
>Could we simply add a line that returns early if op is NULL?
>This yields
>
>extern "C" JNIEXPORT void JNICALL Java_vtk_vtkObjectBase_VTKDelete(JNIEnv *env,jobject obj)
>{
> vtkObjectBase *op;
> op = (vtkObjectBase *)vtkJavaGetPointerFromObject(env,obj,(char *) "vtkObjectBase");
> if (op == NULL) return;
> vtkJavaDeleteObject(env,obj);
> op->Delete();
>}
>
>The following patch achieves this:
>
>--- VTK-4.4.2.orig/Wrapping/vtkWrapJava.c 2003-11-14 15:43:38.000000000 -0500
>+++ VTK-4.4.2/Wrapping/vtkWrapJava.c 2005-10-06 21:32:41.000000000 -0400
>@@ -822,6 +822,7 @@
> fprintf(fp,"{\n %s *op;\n",data->ClassName);
> fprintf(fp," op = (%s *)vtkJavaGetPointerFromObject(env,obj,(char *) \"%s\");\n",
> data->ClassName,data->ClassName);
>+ fprintf(fp," if (op == NULL) return;\n");
> fprintf(fp," vtkJavaDeleteObject(env,obj);\n");
> fprintf(fp," op->Delete();\n");
> fprintf(fp,"}\n");
>
>
>Does this look okay?
>
>-Steve
>
>P.S. The following program blows up with a segfault in unpatched JNI
>code:
>
>
>import vtk.vtkObject;
>
>public class TestDelete
>{
> static
> {
> System.loadLibrary("vtkCommonJava");
> }
>
> public static void main(String[] args)
> {
> vtkObject obj = new vtkObject();
>
> System.out.println( obj.Print() );
> obj.VTKDelete();
> System.out.println( "deleted it" );
> obj.VTKDelete();
> System.out.println( "deleted it" );
> }
>
>}
>
>
>
>
More information about the vtkusers
mailing list