[vtkusers] SmartPointer does not seem to free memory!? Bug!?

David Doria daviddoria+vtk at gmail.com
Wed Dec 9 21:45:41 EST 2009


Thanks,

David



On Wed, Dec 9, 2009 at 9:34 PM,  <itkvtk123 at gmx.net> wrote:
> Hey vtkusers,
>
> today I encountered a problem, that local vtk smartpointers seem not to free their memory after they go out of scope.
> I wrote a minimal example to illustrate my problem:
>
>
> #include "vtkOrientedGlyphContourRepresentation.h"
> #include "vtkImageViewer2.h"
> #include <iostream>
>
> int main()
> {
>        for(int i = 999; i >= 0; i--)
>        {
>                std::cout << i << std::endl;
>
>                vtkOrientedGlyphContourRepresentation* contour = vtkOrientedGlyphContourRepresentation::New();
>                contour->SetRenderer(vtkImageViewer2::New()->GetRenderer());
>        }
>
>        //idle
>        int wait;
>        std::cin >> wait;
>
>        return 0;
> }
>
> This code creates 999 times a local vtk-contour and a local vtkImageviewer and adds the renderer of the viewer to the contour.
> Since all variables are local, I would expect that it gets destroyed after the for-loop is over.
> (actually I would even expect it gets destroyed after each iteration)
>
> But when I have a look at the memory usage, it does not seem to free anything! This for loop (999 times) causes about 130 MB, which are still in use when I reach the input line (std::cin).
>
> Do I miss something?
> Why does something like
>
> void myFunction()
> {
>  vtkImageViewer2::New();
> }
>
> eat up memory???
>
>
> Thanks a lot guys.
> Bye
> --
> GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
> Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>

Unless I'm misunderstanding, you need to actually use smart pointers
if you want them to work!

replace
vtkOrientedGlyphContourRepresentation* contour =
vtkOrientedGlyphContourRepresentation::New();

with
vtkSmartPointer<vtkOrientedGlyphContourRepresentation> contour =
vtkSmartPointer<vtkOrientedGlyphContourRepresentation>::New();

and be sure to #include <vtkSmartPointer.h>

Let us know if that does the trick.

David



More information about the vtkusers mailing list