<br><br>---------- Forwarded message ----------<br><span class="gmail_quote">From: <b class="gmail_sendername">Jeff Lee</b> <<a href="mailto:jeff.lee@us.cd-adapco.com">jeff.lee@us.cd-adapco.com</a>><br>Date: Jun 19, 2007 9:13 AM
<br>Subject: Re: [vtkusers] VTK/Java crashes -- WeakGlobalRef or garbage collection problem?<br>To: Eran Guendelman <<a href="mailto:erang@stanford.edu">erang@stanford.edu</a>><br>Cc: <a href="mailto:vtkusers@vtk.org">
vtkusers@vtk.org</a><br><br></span>If you want the method-scope ref to be valid across jni calls, NewGlobalRef is the one to use, but the java-side object won't be gc'ed even if it is eligible, unless DeleteGlobalRef is called. WeakGlobalRef is supposed to be the solution to that, but with the caveat that the ref could be yanked away from you when the gc runs. In vtk java wrapping, the c++ objects allocated in java aren't deleted until the java object is gc'ed (in the objects finalizer). To get DeleteGlobalRef called from java without relying on garbage collection would require a specific call to something like
obj.VTKDelete(), which is not at all what you would like to do in java. This can all be avoided by altering your programming style a bit by not grabbing references to vtk member objects with method-only scope. This might look like
<br><br>public void setNormalAtLocation(vtkDataArray array, int index, double x, double y, double z) {<br> array.SetTuple3(index, x, y, z);<br>} <br>
<br>to get the normals array, try caching the pointData as an ivar somewhere when you set the vtkPolyData, then it is a matter of <br>pointData.GetArray("XXX"), or pointData.GetNormals(). If you have your own java object which hasa or isa vtkDataSet, then that is probably the right place to do the caching. not the prettiest solution, but it keeps the garbage collector from yanking your temporary reference.
<br><span class="sg">-Jeff</span><div><span class="e" id="q_113441e2fa415f64_2"><br><br><div><span class="gmail_quote">On 6/18/07, <b class="gmail_sendername">Eran Guendelman</b> <<a href="mailto:erang@stanford.edu" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
erang@stanford.edu
</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
We're getting very weird crashes using VTK with Java wrappings. They're<br> weird because they occur at random times during the execution of our<br>program, but when run long enough they always do seem to eventually
<br>occur (e.g. after 2-5 minutes). My main approach to getting it to crash<br> is to start the animation in our application, the animation makes lots<br>of calls to functions such as these<br><br>public void setNormalAtLocation(int index, double x, double y, double z) {
<br> vtkPointData t = pointPolyData.GetPointData();<br> vtkDataArray u = t.GetNormals();<br> u.SetTuple3(index, x, y, z);<br>}<br><br>and<br><br>public void setTensorDataAtLocation(int index, double xx, double xy,
<br>double xz, double yx, double yy, double yz, double zx, double zy, double zz)<br>{<br> vtkPointData t = pointPolyData.GetPointData();<br> t.GetTensors().SetTuple9(index, xx, xy, xz, yx, yy, yz, zx, zy,
<br>
zz);<br>}<br><br>and the crash typically occurs inside one of these functions.<br>pointPolyData is a class member of type vtkPolyData.<br>methods exist.<br><br>I've #defined VTKJAVADEBUG when compiling the (debug) VTK libraries, and
<br>also added other print statements for my own debugging use, and by<br>examining the output I've started to see a pattern in what seems to be<br>causing the crash. Basically, there are two threads that are calling
<br>down to VTK C++ calls... these are the main event thread, and the<br>finalizer thread which does garbage collection. What seems to be<br>happening is that in a call such as<br><br> vtkPointData t = pointPolyData.GetPointData
();<br> vtkDataArray u = t.GetNormals();<br><br>when the GetNormals is getting processed in the C++ side, for some<br>reason the garbage collector seems to think that t can be deallocated,<br>and the finalizer deletes it from under the feet of the main event
<br>thread which expects it to exist... so e.g. we have something like<br><br>t.GetNormals()<br> causes the execution of<br>Java_vtk_vtkDataSetAttributes_GetNormals_120<br> which calls<br>vtkJavaGetPointerFromObject
<br> which calls vtkJavaGetId and gets a valid id for the object, but then<br>gets ptr = 0 and command = 0 for that object because in the meantime the<br>garbage collector has called vtkJavaDeleteObject which deleted it from
<br>the hash table<br><br> So now back in<br>Java_vtk_vtkDataSetAttributes_GetNormals_120<br> it receives a NULL pointer, but assumes it's not NULL and calls<br>(op)->GetNormals()<br> causing a crash!
<br><br>Anyway, in vtkJavaUtil.cxx I found that weak global references were used<br>(NewWeakGlobalRef and DeleteWeakGlobalRef called, at least for<br>JNI_VERSION_1_2 and up which is the case in our install). Admittedly I
<br>don't know much about these creatures, but looking online I found a<br>comment in <a href="http://java.sun.com/j2se/1.4.2/docs/guide/jni/jni-12.html" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
http://java.sun.com/j2se/1.4.2/docs/guide/jni/jni-12.html</a><br>
saying that weak global references are dangerous because they don't<br>count as a real reference as far as garbage collection is concerned, and<br>the type of potential problem mentioned in this document sounded very
<br>
similar to what we are experiencing (objects getting garbage collected<br>during call to native method). So I tried changing to non-weak global<br>refs (NewGlobalRef and DeleteGlobalRef), and indeed found that our<br>crashes seemed to stop (by which I mean I ran it for 10 minutes without
<br>crashing which never happened before... doesn't conclusively mean the<br>crash is fixed, but seems to be a positive change).<br><br><br>So has anyone else experienced a similar problem? Anyone know whether<br>the use of weak global references by VTK/Java is indeed a mistake, or
<br>should they be working and we just have some other problem with our code?<br><br>Any help would be greatly appreciated,<br><br>Eran.<br><br><br>_______________________________________________<br>This is the private VTK discussion list.
<br>Please keep messages on-topic. Check the FAQ at: <a href="http://www.vtk.org/Wiki/VTK_FAQ" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://www.vtk.org/Wiki/VTK_FAQ</a><br>Follow this link to subscribe/unsubscribe:
<br><a href="http://www.vtk.org/mailman/listinfo/vtkusers" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
http://www.vtk.org/mailman/listinfo/vtkusers</a><br></blockquote></div><br>
</span></div>