[vtkusers] is VTKDelete() intended for public use?

Steve M. Robbins steven.robbins at videotron.ca
Thu Oct 6 22:38:11 EDT 2005


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