<!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 &Action)</FONT>
<BR><FONT SIZE=2>{</FONT>
<BR><FONT SIZE=2> if (shrink) shrink->Delete();</FONT>
<BR><FONT SIZE=2> vtkTimerLog::DeallocateTimerLog();</FONT>
<BR><FONT SIZE=2> vtkWindow1->GetRenderer()->RemoveAllProps();</FONT>
<BR><FONT SIZE=2> // VTK_Form->Release();</FONT>
<BR><FONT SIZE=2> // Application->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. 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. You could go to some sort of lazy create if</FONT>
<BR><FONT SIZE=2>there is no such method. Basically, the first time the vtk object</FONT>
<BR><FONT SIZE=2>is used, then create the vtk object. 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>>Hi all,</FONT>
<BR><FONT SIZE=2>></FONT>
<BR><FONT SIZE=2>>I believe I have found some flaws in the vtk code.</FONT>
<BR><FONT SIZE=2>>I think most vtk users are not affected, but us poor souls that are </FONT>
<BR><FONT SIZE=2>>forced to use Borland C++ Builder (BCB) are affected.</FONT>
</P>
<P><FONT SIZE=2>>The problem reveals itself the following way (in my case at least): </FONT>
<BR><FONT SIZE=2>>Access violation in the destructor of vtkPolyData (~vtkPolyData) - The </FONT>
<BR><FONT SIZE=2>>DummyCritSect.Lock() call fails.</FONT>
<BR><FONT SIZE=2>></FONT>
<BR><FONT SIZE=2>>The cause is that DummyCritSect, in certain cases, has already been </FONT>
<BR><FONT SIZE=2>>destroyed. In other words: ~vtkPolyData is calling a method in an </FONT>
<BR><FONT SIZE=2>>object that is no longer in existence.</FONT>
<BR><FONT SIZE=2>></FONT>
<BR><FONT SIZE=2>>Explanation:</FONT>
<BR><FONT SIZE=2>>DummyCritSect is a "nonlocal" object which is created before main() is </FONT>
<BR><FONT SIZE=2>>executed, and destroyed after main() has returned. This is actually </FONT>
<BR><FONT SIZE=2>>what is intended, and quite nice, except when used from within (but not </FONT>
<BR><FONT SIZE=2>>exclusively) BCB...</FONT>
<BR><FONT SIZE=2>></FONT>
<BR><FONT SIZE=2>>The problem is that the Visual Component Library (VCL), which is the </FONT>
<BR><FONT SIZE=2>>foundation of BCB, also seems to be created from a "nonlocal" object. </FONT>
<BR><FONT SIZE=2>>This means that BCB Windows and DummyCritSect are created at the same </FONT>
<BR><FONT SIZE=2>>point, and destroyed at the same point, but the sequence is undefined.</FONT>
<BR><FONT SIZE=2>></FONT>
<BR><FONT SIZE=2>>According to "The C++ Programming Language" by Bjarne Stroustrup:</FONT>
<BR><FONT SIZE=2>>-------</FONT>
<BR><FONT SIZE=2>>Section 10.4.9 Nonlocal store:</FONT>
<BR><FONT SIZE=2>></FONT>
<BR><FONT SIZE=2>>1) "A variable defined outside any function (that is, global, </FONT>
<BR><FONT SIZE=2>>namespace, and class static variables; §C.9) is initialized </FONT>
<BR><FONT SIZE=2>>(constructed) before main() is invoked, and any such variable that has </FONT>
<BR><FONT SIZE=2>>been constructed will have its destructor invoked after exit from </FONT>
<BR><FONT SIZE=2>>main()."</FONT>
<BR><FONT SIZE=2>></FONT>
<BR><FONT SIZE=2>>2) "No implementation-independent guarantees are made about the order </FONT>
<BR><FONT SIZE=2>>of construction of nonlocal objects in different compilation units."</FONT>
<BR><FONT SIZE=2>></FONT>
<BR><FONT SIZE=2>>3) "The order of destruction is similarly implementation-dependent."</FONT>
<BR><FONT SIZE=2>>-------</FONT>
<BR><FONT SIZE=2>></FONT>
<BR><FONT SIZE=2>>This means that IF vtk is accessed from a "nonlocal" object, it is </FONT>
<BR><FONT SIZE=2>>implementation-dependent wether it works or not. In case of BCB, it </FONT>
<BR><FONT SIZE=2>>doesn't :-(</FONT>
<BR><FONT SIZE=2>></FONT>
<BR><FONT SIZE=2>>As long as you are not making components containing vtk-code in BCB, it </FONT>
<BR><FONT SIZE=2>>is possible to make workarounds, but it seems to be impossible to get </FONT>
<BR><FONT SIZE=2>>component-wrapped vtk-code to work correctly.</FONT>
<BR><FONT SIZE=2>></FONT>
<BR><FONT SIZE=2>></FONT>
<BR><FONT SIZE=2>>How do we get around this problem?</FONT>
<BR><FONT SIZE=2>>I don't think that we should "#ifdef __BORLANDC__" our way out of the </FONT>
<BR><FONT SIZE=2>>BCB problem, this will leave the code flawed for projects using </FONT>
<BR><FONT SIZE=2>>nonlocal objects. Any thoughts?</FONT>
<BR><FONT SIZE=2>></FONT>
<BR><FONT SIZE=2>></FONT>
<BR><FONT SIZE=2>>Best regards</FONT>
<BR><FONT SIZE=2>>Per</FONT>
<BR><FONT SIZE=2>></FONT>
</P>
</BODY>
</HTML>