[vtkusers] Segfault when using CreateTimer()

Torquil Macdonald Sørensen torquil at gmail.com
Tue Jul 1 06:32:17 EDT 2008


Hello, can anybody with a trained VTK-eye see why the program segfaults?

I am trying to have a callback function so that a simulation can proceed at 
the same time as I use the mouse to interact with the plot. I have tried to 
adapt an example that I found, but I get a segfault as soon as I try to use 
CreateTimer() inside the callback function.

I use CreateTimer() after renderWindowInteractor->Initialize() in main(), and 
that sees to work correctly since the program afterwards enters the callback 
function. But when the callback function calles CreateTimer(0), it segfaults. 
This is on Linux with VTK 5.0.4.

Gdb, Valgrind, and the source is below:

*** The output from gbd is:

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1208645904 (LWP 32354)]
myTimerCallback (caller=0x8bdd0e8, eid=25, clientdata=0x0, calldata=0x0)
    at timer.cpp:29
29              iren->CreateTimer(0); // <---- Segfault here



*** Valgrind told me something like this:

==2063== Invalid read of size 4
==2063==    at 0x8048AAB: myTimerCallback(vtkObject*, unsigned long, void*, 
void*) (timer.cpp:29)
==2063==    by 0x4EAAE05: vtkCallbackCommand::Execute(vtkObject*, unsigned 
long, void*) (in /mn/anatu/cma-u3/tma/usr/stow/vtk/lib/libvtkCommon.so.5.0.4)
==2063==    by 0x4F391FD: vtkSubjectHelper::InvokeEvent(unsigned long, void*, 
vtkObject*) (in /mn/anatu/cma-u3/tac/usr/stow/vtk/lib/libvtkCommon.so.5.0.4)
==2063==    by 0x4F392FE: vtkObject::InvokeEvent(unsigned long, void*) 
(in /mn/anatu/cma-u3/tmac/usr/stow/vtk/li/libvtkCommon.so.5.0.4)
==2063==    by 0x4180320: vtkXRenderWindowInteractorTimer(void*, unsigned 
long*) (in /mn/anatu/cma-u3/tmac/usr/sow/vtk/lib/libvtkRendering.so.5.0.4)
==2063==    by 0x70E0F61: (within /usr/lib/libXt.so.6.0.0)
==2063==    by 0x70E10A1: XtAppNextEvent (in /usr/lib/libXt.so.6.0.0)
==2063==    by 0x4181E9C: vtkXRenderWindowInteractor::Start() 
(in /mn/anatu/cma-u3/tmac/usr/stow/vtk/lib/libvtkRndering.so.5.0.4)
==2063==    by 0x8048C9D: main (timer.cpp:59)



*** Here is a stripped-down version of my program:

#include <vtk-5.0/vtkActor.h>
#include <vtk-5.0/vtkCallbackCommand.h>
#include <vtk-5.0/vtkCamera.h>
#include <vtk-5.0/vtkInteractorStyle.h>
#include <vtk-5.0/vtkOutlineSource.h>
#include <vtk-5.0/vtkPolyDataMapper.h>
#include <vtk-5.0/vtkRenderer.h>
#include <vtk-5.0/vtkRenderWindow.h>
#include <vtk-5.0/vtkRenderWindowInteractor.h>

using namespace std;

static const int time_max=20;

void myTimerCallback(vtkObject* caller, unsigned long eid, void* clientdata,
					 void* calldata) {
	vtkInteractorStyle* istyle = (vtkInteractorStyle *) caller;
	vtkRenderWindowInteractor* iren = istyle->GetInteractor();
	static int time = 0;

// 	if (++time < time_max) {
//		// Simulation steps here 
// 		cout << time << endl;
// 		iren->CreateTimer(0);
// 		return; 
// 	}

	time = 0;
	iren->CreateTimer(0); // <---- Segfault here
}

int main()
{
	vtkRenderWindow* renderWindow = vtkRenderWindow::New();
	renderWindow->SetSize(400, 400);

	vtkRenderWindowInteractor* renderWindowInteractor = 
vtkRenderWindowInteractor::New();
	renderWindow->SetInteractor(renderWindowInteractor);
	
	vtkRenderer* renderer= vtkRenderer::New();
	renderer->SetBackground(0.0, 0.0, 0.0);
	renderWindow->AddRenderer(renderer);

	vtkOutlineSource* outlineSource = vtkOutlineSource::New();
	outlineSource->SetBounds(-1, 1, -1, 1, -1, 1);
	vtkPolyDataMapper* outlineMapper = vtkPolyDataMapper::New();
	outlineMapper->SetInput(outlineSource->GetOutput());
	vtkActor* outlineActor = vtkActor::New();
	outlineActor->SetMapper(outlineMapper);
	renderer->AddActor(outlineActor);

	vtkCallbackCommand* timerCB = vtkCallbackCommand::New();
	timerCB->SetCallback(myTimerCallback);
	renderWindowInteractor->AddObserver(vtkCommand::TimerEvent, timerCB);
	
	renderWindowInteractor->Initialize();
	renderWindowInteractor->CreateTimer(0);
	renderWindowInteractor->Start();

	outlineActor->Delete(); outlineMapper->Delete(); outlineSource->Delete();
	renderer->Delete(); renderWindowInteractor->Delete(); renderWindow->Delete();

	return(0);
}




More information about the vtkusers mailing list