<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2653.12">
<TITLE>RE: [vtkusers] Serious(?) flaws in vtk code</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=2>Dear all,</FONT>
</P>

<P><FONT SIZE=2>I have been using BCB6 + VTK 4.2 and had similar problems before.</FONT>
<BR><FONT SIZE=2>To solve Access violation problem related to DummyCritSect.Lock() in the destructor of vtkPolyData (~vtkPolyData), I did these in the Project_vtkDemo:</FONT></P>

<P><FONT SIZE=2>1) Remove everything in the FormDestroy event handler.</FONT>
<BR><FONT SIZE=2>2) Add this FormClose event handler. All problems should be gone!!</FONT>
</P>

<P><FONT SIZE=2>void __fastcall TVTK_Form::FormClose(TObject *Sender, TCloseAction &amp;Action)</FONT>
<BR><FONT SIZE=2>{</FONT>
<BR><FONT SIZE=2>&nbsp; if (shrink) shrink-&gt;Delete();</FONT>
<BR><FONT SIZE=2>&nbsp; vtkTimerLog::DeallocateTimerLog();</FONT>
<BR><FONT SIZE=2>&nbsp; vtkWindow1-&gt;GetRenderer()-&gt;RemoveAllProps();</FONT>
<BR><FONT SIZE=2>&nbsp; // VTK_Form-&gt;Release();</FONT>
<BR><FONT SIZE=2>&nbsp; // Application-&gt;Terminate();</FONT>
<BR><FONT SIZE=2>}</FONT>
<BR><FONT SIZE=2>I am not sure if the last two lines of code are required in BCB6.</FONT>
</P>

<P><FONT SIZE=2>vtkTimerLog::DeallocateTimerLog() is a fix discussed here:</FONT>
<BR><FONT SIZE=2><A HREF="http://public.kitware.com/pipermail/vtkusers/2003-August/019522.html" TARGET="_blank">http://public.kitware.com/pipermail/vtkusers/2003-August/019522.html</A></FONT>
</P>
<BR>

<P><FONT SIZE=2>Xianjin Yang, Ph.D.</FONT>
</P>
<BR>
<BR>
<BR>

<P><FONT SIZE=2>-----Original Message-----</FONT>
<BR><FONT SIZE=2>From: William A. Hoffman [<A HREF="mailto:billlist@nycap.rr.com">mailto:billlist@nycap.rr.com</A>] </FONT>
<BR><FONT SIZE=2>Sent: Tuesday, March 09, 2004 10:18 AM</FONT>
<BR><FONT SIZE=2>To: Per Dalgas Jakobsen; vtkusers@vtk.org</FONT>
<BR><FONT SIZE=2>Subject: Re: [vtkusers] Serious(?) flaws in vtk code</FONT>
</P>
<BR>

<P><FONT SIZE=2>I have not looked at the BCB Windows code, however,</FONT>
<BR><FONT SIZE=2>it seems that you might be able to move the creation/destruction</FONT>
<BR><FONT SIZE=2>of the vtk objects to a different spot.&nbsp;&nbsp; There must be some method</FONT>
<BR><FONT SIZE=2>that is called to initialize the BCB objects, and another one</FONT>
<BR><FONT SIZE=2>to destroy them.&nbsp;&nbsp; You could go to some sort of lazy create if</FONT>
<BR><FONT SIZE=2>there is no such method.&nbsp;&nbsp; Basically, the first time the vtk object</FONT>
<BR><FONT SIZE=2>is used, then create the vtk object.&nbsp; Then when your window gets the close event, destroy the vtk object, don't just create and destroy the vtk objects in the constructor/destructor of the BCB object. All vtk objects should be created after main, and deleted before the end of main, and you should have no problem.</FONT></P>

<P><FONT SIZE=2>-Bill</FONT>
</P>
<BR>

<P><FONT SIZE=2>At 04:19 AM 3/9/2004, Per Dalgas Jakobsen wrote:</FONT>
<BR><FONT SIZE=2>&gt;Hi all,</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt;I believe I have found some flaws in the vtk code.</FONT>
<BR><FONT SIZE=2>&gt;I think most vtk users are not affected, but us poor souls that are </FONT>
<BR><FONT SIZE=2>&gt;forced to use Borland C++ Builder (BCB) are affected.</FONT>
</P>

<P><FONT SIZE=2>&gt;The problem reveals itself the following way (in my case at least): </FONT>
<BR><FONT SIZE=2>&gt;Access violation in the destructor of vtkPolyData (~vtkPolyData) - The </FONT>
<BR><FONT SIZE=2>&gt;DummyCritSect.Lock() call fails.</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt;The cause is that DummyCritSect, in certain cases, has already been </FONT>
<BR><FONT SIZE=2>&gt;destroyed. In other words: ~vtkPolyData is calling a method in an </FONT>
<BR><FONT SIZE=2>&gt;object that is no longer in existence.</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt;Explanation:</FONT>
<BR><FONT SIZE=2>&gt;DummyCritSect is a &quot;nonlocal&quot; object which is created before main() is </FONT>
<BR><FONT SIZE=2>&gt;executed, and destroyed after main() has returned. This is actually </FONT>
<BR><FONT SIZE=2>&gt;what is intended, and quite nice, except when used from within (but not </FONT>
<BR><FONT SIZE=2>&gt;exclusively) BCB...</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt;The problem is that the Visual Component Library (VCL), which is the </FONT>
<BR><FONT SIZE=2>&gt;foundation of BCB, also seems to be created from a &quot;nonlocal&quot; object. </FONT>
<BR><FONT SIZE=2>&gt;This means that BCB Windows and DummyCritSect are created at the same </FONT>
<BR><FONT SIZE=2>&gt;point, and destroyed at the same point, but the sequence is undefined.</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt;According to &quot;The C++ Programming Language&quot; by Bjarne Stroustrup:</FONT>
<BR><FONT SIZE=2>&gt;-------</FONT>
<BR><FONT SIZE=2>&gt;Section 10.4.9 Nonlocal store:</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt;1) &quot;A variable defined outside any function (that is, global, </FONT>
<BR><FONT SIZE=2>&gt;namespace, and class static variables; §C.9) is initialized </FONT>
<BR><FONT SIZE=2>&gt;(constructed) before main() is invoked, and any such variable that has </FONT>
<BR><FONT SIZE=2>&gt;been constructed will have its destructor invoked after exit from </FONT>
<BR><FONT SIZE=2>&gt;main().&quot;</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt;2) &quot;No implementation-independent guarantees are made about the order </FONT>
<BR><FONT SIZE=2>&gt;of construction of nonlocal objects in different compilation units.&quot;</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt;3) &quot;The order of destruction is similarly implementation-dependent.&quot;</FONT>
<BR><FONT SIZE=2>&gt;-------</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt;This means that IF vtk is accessed from a &quot;nonlocal&quot; object, it is </FONT>
<BR><FONT SIZE=2>&gt;implementation-dependent wether it works or not. In case of BCB, it </FONT>
<BR><FONT SIZE=2>&gt;doesn't :-(</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt;As long as you are not making components containing vtk-code in BCB, it </FONT>
<BR><FONT SIZE=2>&gt;is possible to make workarounds, but it seems to be impossible to get </FONT>
<BR><FONT SIZE=2>&gt;component-wrapped vtk-code to work correctly.</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt;How do we get around this problem?</FONT>
<BR><FONT SIZE=2>&gt;I don't think that we should &quot;#ifdef __BORLANDC__&quot; our way out of the </FONT>
<BR><FONT SIZE=2>&gt;BCB problem, this will leave the code flawed for projects using </FONT>
<BR><FONT SIZE=2>&gt;nonlocal objects. Any thoughts?</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt;Best regards</FONT>
<BR><FONT SIZE=2>&gt;Per</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
</P>

</BODY>
</HTML>