<br><br>---------- Forwarded message ----------<br><span class="gmail_quote">From: <b class="gmail_sendername">Jeff Lee</b> &lt;<a href="mailto:jeff.lee@us.cd-adapco.com">jeff.lee@us.cd-adapco.com</a>&gt;<br>Date: Jun 19, 2007 9:13 AM
<br>Subject: Re: [vtkusers] VTK/Java crashes -- WeakGlobalRef or garbage collection problem?<br>To: Eran Guendelman &lt;<a href="mailto:erang@stanford.edu">erang@stanford.edu</a>&gt;<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&#39;t be gc&#39;ed even if it is eligible, unless DeleteGlobalRef is called.&nbsp; 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.&nbsp; In vtk java wrapping, the c++ objects allocated in java aren&#39;t deleted until the java object is gc&#39;ed (in the objects finalizer).&nbsp; 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.&nbsp; This can all be avoided by altering your programming style a bit by not grabbing references to vtk member objects with method-only scope.&nbsp; This might look like
<br><br>public void setNormalAtLocation(vtkDataArray array, int index, double x, double y, double z) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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(&quot;XXX&quot;), or pointData.GetNormals().&nbsp; If you have your own java object which hasa or isa vtkDataSet, then that is probably the right place to do the caching.&nbsp; 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> &lt;<a href="mailto:erang@stanford.edu" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
erang@stanford.edu
</a>&gt; 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&#39;re getting very weird crashes using VTK with Java wrappings.&nbsp;&nbsp;They&#39;re<br>&nbsp;&nbsp;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).&nbsp;&nbsp;My main approach to getting it to crash<br>&nbsp;&nbsp;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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vtkPointData t = pointPolyData.GetPointData();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vtkDataArray u = t.GetNormals();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vtkPointData t = pointPolyData.GetPointData();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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&#39;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&#39;ve started to see a pattern in what seems to be<br>causing the crash.&nbsp;&nbsp;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.&nbsp;&nbsp;What seems to be<br>happening is that in a call such as<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vtkPointData t = pointPolyData.GetPointData






();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;causes the execution of<br>Java_vtk_vtkDataSetAttributes_GetNormals_120<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;which calls<br>vtkJavaGetPointerFromObject
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;So now back in<br>Java_vtk_vtkDataSetAttributes_GetNormals_120<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;it receives a NULL pointer, but assumes it&#39;s not NULL and calls<br>(op)-&gt;GetNormals()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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).&nbsp;&nbsp;Admittedly I
<br>don&#39;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&#39;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).&nbsp;&nbsp;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...&nbsp;&nbsp;doesn&#39;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?&nbsp;&nbsp;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>