[vtkusers] possible BUG: segmentation fault by deleting a vtkRenderer
Thomas Faust
tf at thermoanalytics.com
Tue Dec 21 07:46:01 EST 2004
Hi vtkusers.
I found a problem in the application I am working with.
I changed the sequence of creating and destroying objects and found a
segmentation fault that could be a possible bug and should be removed.
My program was creating a renderer and a renderwindow and all used vtk
objects (interactor, mapper, data, filter...) after this. Now it changed:
I create a renderer and all vtk objects and assemble the graphic pipeline
without my GUI. After this I create a (offscreen) renderwindow add the
renderer, render a image, remove the renderer and delete the renderwindow
again.
The problem results from a invalid pointer to a renderwindow after
deleting it from the renderer.
The function vtkRenderWindow::RemoveRenderer() should be extended to set
Renderer::RenderWindow to NULL. This will avoid the segmentation fault
when deleting the renderer (when the renderwindow is already gone)
// Remove a renderer from the list of renderers.
void vtkRenderWindow::RemoveRenderer(vtkRenderer *ren)
{
ren->SetRenderWindow(NULL); // !!!! don't forget the remove the
renderwindow from the renderer
this->Renderers->RemoveItem(ren);
}
try the following code to reproduce the problem:
#include "vtkSphereSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkProperty.h"
int main ()
{
// create sphere geometry
vtkSphereSource *sphere = vtkSphereSource::New();
sphere->SetRadius(1.0);
sphere->SetThetaResolution(18);
sphere->SetPhiResolution(18);
// map to graphics library
vtkPolyDataMapper *map = vtkPolyDataMapper::New();
map->SetInput(sphere->GetOutput());
sphere->Delete(); // sphere as referernce in mapper -> not needed here
anymore
// actor coordinates geometry, properties, transformation
vtkActor *aSphere = vtkActor::New();
aSphere->SetMapper(map);
aSphere->GetProperty()->SetColor(0,0,1); // sphere color blue
map->Delete(); // mapper has reference in actor
// a renderer and render window
vtkRenderer *ren1 = vtkRenderer::New();
// add the actor to the scene
ren1->AddActor(aSphere);
ren1->SetBackground(1,1,1); // Background color white
aSphere->Delete(); // actor has reference in renderer -> not needed
here anymore
// create a offscreen render window to write a picture
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->OffScreenRenderingOn();
renWin->AddRenderer(ren1);
// render an image (lights and cameras are created automatically)
renWin->Render();
// write the picture with windowtoimage filter
renWin->RemoveRenderer(ren1); // !!!!!!!!!! BUG !!!!!!!!!!!
renWin->Delete();
// Segantation Fault because the ren1->RenderWindow is invalif pointer
ren1->Delete();
return 0;
}
More information about the vtkusers
mailing list