<HTML>
<HEAD>
<TITLE>Re: [vtk-developers] [coding tip] no call to Set in constructors</TITLE>
</HEAD>
<BODY>
<FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>A slight qualification: it is OK to call the Set function in the constructors _<I>so long as you initialize the underlying variable first_</I>.  I find I do this frequently with the string and object ivars to ensure that the memory management happens correctly.  For example:<BR>
<BR>
</SPAN></FONT><FONT SIZE="2"><FONT FACE="Consolas, Courier New, Courier"><SPAN STYLE='font-size:10pt'>this->FileName = NULL;<BR>
this->SetFileName("/tmp/log");<BR>
</SPAN></FONT></FONT><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
or<BR>
<BR>
</SPAN></FONT><FONT SIZE="2"><FONT FACE="Consolas, Courier New, Courier"><SPAN STYLE='font-size:10pt'>this->Controller = NULL;<BR>
this->SetController(vtkMultiProcessController::GetGlobalController());<BR>
</SPAN></FONT></FONT><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
Of course, setting the ivar to NULL first is vital, or things could potentially die a really horrible death.<BR>
<BR>
-Ken<BR>
<BR>
<BR>
On 4/29/09 7:31 AM, "Francois Bertel" <<a href="francois.bertel@kitware.com">francois.bertel@kitware.com</a>> wrote:<BR>
<BR>
</SPAN></FONT><BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>Hello,<BR>
<BR>
I've seen that more often in recent additions to VTK: calls to some<BR>
Set methods in the default constructor.<BR>
<BR>
I would like to stress that Set methods defined with vtkSetMacro (it<BR>
might be OK for other Set methods) cannot be used in the default<BR>
constructor to initialize ivars because the first line of a Set method<BR>
is to compare the current value of the ivar with the value in<BR>
argument. As at this point (in the constructor), the ivar is not<BR>
initialized yet, the comparison happens against an uninitialized<BR>
value.<BR>
<BR>
valgrind will detect this kind of fault.<BR>
<BR>
For this reason, in the constructor, the value of an ivar has to be<BR>
initialized directly with the assignment operator not through a Set<BR>
method defined with vtkSetMacro.<BR>
<BR>
Example:<BR>
<BR>
class vtkFoo : public vtkObject<BR>
{<BR>
 public:<BR>
...<BR>
  vtkGetMacro(X,int);<BR>
  vtkSetMacro(X,int);<BR>
...<BR>
 protected:<BR>
  vtkFoo();<BR>
  int X;<BR>
...<BR>
};<BR>
<BR>
vtkFoo::vtkFoo<BR>
{<BR>
 this->SetX(12); // neh<BR>
}<BR>
<BR>
vtkFoo::vtkFoo<BR>
{<BR>
 this->X=12; // good<BR>
}<BR>
<BR>
The issue looks pretty obvious in an isolated example like the one<BR>
shown above. Sometimes it can also happen with an indirect call:<BR>
<BR>
void vtkFoo::SetXToZero()<BR>
{<BR>
 this->SetX(0);<BR>
}<BR>
<BR>
vtkFoo::vtkFoo<BR>
{<BR>
 this->SetXToZero(); // neh<BR>
}<BR>
<BR>
I added a section addressing this issue in the VTK Coding Standards:<BR>
<a href="http://www.vtk.org/Wiki/VTK_Coding_Standards#Set_method_in_constructor">http://www.vtk.org/Wiki/VTK_Coding_Standards#Set_method_in_constructor</a><BR>
<BR>
<BR>
<BR>
Here is a real case in VTK, detected by valgrind:<BR>
<BR>
<a href="http://www.cdash.org/CDash/viewDynamicAnalysisFile.php?id=325444">http://www.cdash.org/CDash/viewDynamicAnalysisFile.php?id=325444</a><BR>
<BR>
vtkRenderView::vtkRenderView() calls SetInteractionModeTo3D(), which<BR>
calls SetInteractionMode(), but ivar InteractionMode has not been<BR>
initialized yet.<BR>
<BR>
<BR>
<BR>
--<BR>
François Bertel, PhD  | Kitware Inc. Suite 204<BR>
1 (518) 371 3971 x113 | 28 Corporate Drive<BR>
                      | Clifton Park NY 12065, USA<BR>
_______________________________________________<BR>
Powered by www.kitware.com<BR>
<BR>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html">http://www.kitware.com/opensource/opensource.html</a><BR>
<BR>
Follow this link to subscribe/unsubscribe:<BR>
<a href="http://www.vtk.org/mailman/listinfo/vtk-developers">http://www.vtk.org/mailman/listinfo/vtk-developers</a><BR>
<BR>
<BR>
<BR>
</SPAN></FONT></BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
</SPAN></FONT><FONT SIZE="2"><FONT FACE="Consolas, Courier New, Courier"><SPAN STYLE='font-size:10pt'><BR>
   ****      Kenneth Moreland<BR>
    ***      Sandia National Laboratories<BR>
***********  <BR>
*** *** ***  email: <a href="kmorel@sandia.gov">kmorel@sandia.gov</a><BR>
**  ***  **  phone: (505) 844-8919<BR>
    ***      web:   <a href="http://www.cs.unm.edu/~kmorel">http://www.cs.unm.edu/~kmorel</a><BR>
</SPAN></FONT></FONT><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
</SPAN></FONT>
</BODY>
</HTML>