[vtkusers] memory leak in vtkConstrainedPointHandleRepresentation VTK 5.8.0

Alex Malyushytskyy alexmalvtk at gmail.com
Wed May 9 19:28:05 EDT 2012


I attached modification of cube example in order to reproduce the leak.
In addition you need to link against widget.lib which original cube
does not have as dependency listed.

After you start executable and see the cube,
click on it and draw the distant widget.

If you exit now you will see a plenty of leaks in:
vtkMatrix
vtkAssemblyPath
vtkAssemblyNode
vtkTransform


To fix these leaks you need to patch
vtkConstrainedPointHandleRepresentation::GetIntersectionPosition
as below.

All lines changed or important are commented
and all comments are mine.

This definitely fixes leaks for me.
If anybody could patch official/developer version I would appreciate.

Best regards,
    Alex.

int vtkConstrainedPointHandleRepresentation::GetIntersectionPosition(double
eventPos[2],

double worldPos[3],

double tolerance,

vtkRenderer * renderer)
{
 ... ...  ...
  vtkCellPicker *picker = vtkCellPicker::New();

  picker->Pick(eventPos[0], eventPos[1], 0, renderer);

//  why not to use smart pointer below ???

  vtkAssemblyPath *path = picker->GetPath();

  if(path == 0)
   {

// since smart pointer is not used add line below

   picker->Delete();
   return 0;
   }
  double pickPos[3];
  picker->GetPickPosition(pickPos);
  path->Register(this);
  if ( this->BoundingPlanes )
    {
    vtkPlane *p;
    this->BoundingPlanes->InitTraversal();
    while ( (p = this->BoundingPlanes->GetNextItem()) )
      {
      double v = p->EvaluateFunction( pickPos );
      if ( v < tolerance )
        {
        return 0;
        }
      }
    }

  worldPos[0] = pickPos[0];
  worldPos[1] = pickPos[1];
  worldPos[2] = pickPos[2];

// as I understand the code path->Register(this);
//  added reference count
// I am not sure if it is necessary in the first place,
// but if it is done we need to remove reference counter
// to prevent memory leak,
// which is done at the next line
  path->Delete();

  picker->Delete();

  return 1;
}




On Tue, May 8, 2012 at 9:35 PM, Alex Malyushytskyy <alexmalvtk at gmail.com> wrote:
> Actually to get memory leak with code below you need to start interaction,
> so add the following after the code below :
>
> distanceWidget->On();
>
> and click to add points,
>
> It looks like memory leak is related to the Glyph3D usage,
> but I could not find an exact place where it happens,
>
> Any help is appreciated.
>
> Alex
>
>
> On Mon, May 7, 2012 at 8:35 PM, Alex Malyushytskyy <alexmalvtk at gmail.com> wrote:
>> It looks like that there is a memory leak in
>> vtkConstrainedPointHandleRepresentation .
>> ( at least VTK 5.8.0, MSVS2010)
>>
>> I am trying to use vtkDistanceWidget which is initialized as:
>>
>>        vtkSmartPointer< vtkConstrainedPointHandleRepresentation > handleRep
>> = vtkSmartPointer< vtkConstrainedPointHandleRepresentation >::New();
>>        vtkSmartPointer< vtkDistanceRepresentation2D  > distanceRep =
>> vtkSmartPointer< vtkDistanceRepresentation2D >::New();
>>
>>        distanceRep->SetHandleRepresentation( handleRep );
>>
>>        distanceRep->SetLabelFormat("%-#6.5g in");
>>        distanceRep->RulerModeOff(  );
>>        distanceRep->SetRulerDistance( 120. );
>>
>>        distanceWidget = vtkSmartPointer<vtkDistanceWidget>::New();
>>        distanceWidget->SetInteractor( GetRenderWindow()->GetInteractor() );
>>        distanceWidget->SetRepresentation( distanceRep );
>>        distanceRep->InstantiateHandleRepresentation();
>>        distanceWidget->CreateDefaultRepresentation();
>>        distanceWidget->EnabledOff();
>>
>>
>> When application exits I get VTK notification about memory leaks.
>> If I replace vtkConstrainedPointHandleRepresentation with
>> vtkPointHandleRepresentation2D
>> there is no memory leak (but I do not get default behavior)
>>
>> Could anybody who has access to nightly version check if there were any patches?
>>
>> Regards,
>>  Alex
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Cube.cxx
Type: application/octet-stream
Size: 3875 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120509/028e70c0/attachment.obj>


More information about the vtkusers mailing list