[vtkusers] please help me, iam getting a message saying "trying to delete a object with non zero reference coun

david michell davidmichell at rediffmail.com
Fri Jan 23 04:36:19 EST 2004


An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20040123/6eebfdc9/attachment.htm>
-------------- next part --------------
hello everybody,
thanks for the all the help and wonderful code.

iam writing a class to implement moving a line.

i basically ripped the code from the linewidget.

i have one problem, when I destroy the object in the main() by using delete, iam getting a message saying "trying
to delete object with non zero reference count"

i traced through the program and found out that the object was clientdata, from the 
EventCallbackCommand.

i dont know what to do with this information.

what should i do to the code.

also if i do this->ReferenceCount-- in the destructor, then the program works correctly.

mine is vtk42 and VC++ 7 .net enabled

please help me.

thanking you,
david michell



//all the header files go here
class grids : public vtkInteractorObserver
{
BOOL m_mousedown;
vtkRenderer *m_renderer;
vtkLineSource *m_linesource;
vtkPolyDataMapper *m_datamapper;
vtkActor *m_actor;
float m_lastpickposition[4];

public:
grids(vtkRenderer *renderer)
{
	m_renderer=renderer;
	m_linesource=vtkLineSource::New();
	m_datamapper=vtkDataMapper::New();
	m_actor=vtkActor::New();
	m_picker=vtkCellPicker::New();
	m_linesource.Point1(0.0,0.0,0.0);
	m_linesource.Point1(0.0,10.0,0.0);
	m_datamapper->SetInput(linesource->GetOutput());
	m_actor->SetMapper(m_datamapper);
	m_picker->AddPickList(m_actor);
        m_renderer->AddActor(m_actor);
	m_renderer->Render();
	this->EventCallbackCommand->SetCallback(grids::ProcessEvents);

}

~grids()
{
//this->ReferenceCount--;   //if i uncomment this line
                           // then the program works fine
                           //if i comment 
                           //this line the program displays a
                           //message saying "trying to delete
                          //an object with non zero refernce count 

	m_actor->Delete();
	m_datamapper->Delete();
	m_linesource->Delete();
	m_picker->Delete();

}

static void ProcessEvents(vtkObject object,unsigned long event, void *clientdata,void *calldata)
{

grid *self=reinterpret_cast<grid *>(clientdata);

switch(event)
{
	case vtkCommand::LeftButtonPressEvent:
		self->OnLeftButtonDown();
	break;
			
	case vtkCommand::LeftButtonReleaseEvent:
		self->OnLeftButtonUp();
	break;
	
	case vtkCommand::MouseMoveEvent:
		self->OnMouseMove();
	break;
}
}

void SetEnabled(int enabling)
{
	if (!this->Interactor) return;
	
	if (enabling)
	{
		if (this->Enabled) return;
		this->Enabled=1;
		this->CurrentRenderer=m_renderer;
		
		vtkRenderWindowInteractor *i=this->Interactor;
		i->AddObserver(vtkCommand::MouseMoveEvent,
			this->EventCallbackCommand,this->priority);
		i->AddObserver(vtkCommand::LeftButtonPressEvent,
			this->EventCallbackCommand,this->priority);
		i->AddObserver(vtkCommand::LeftButtonReleaseEvent,
			this->EventCallbackCommand,this->priority);
		
		this->InvokeEvent(vtkCommand::EnableEvent,NULL);
	}
	else
	{
		if (!this->Enabled) return;
		this->Enabled=0;
		this->CurrentRenderer=NULL;
	this->Interactor->RemoveObserver(this->EventCallbackCommand);
		this->InvokeEvent(vtkCommand::DisableEvent,NULL);
	}
	
	this->Interactor->Render();
}
		
	
	

void OnLeftButtonDown()
{
	int x=this->Interactor->GetEventPosition()[0];
	int y=this->Interactor->GetEventPosition()[1];
	
	vtkAssemblyPath *path;
	
	this->m_picker->Pick(x,y,0.0,this->m_renderer);
	path=this->m_picker->GetPath();
	
	if (path!=NULL)
	
	{
		m_picker->GetPickPosition(m_lastpickposition);
		m_mousedown=TRUE;
		this->InvokeEvent(vtkCommand::StartInteractionEvent,NULL);
	}
	
	else 
		return;
		
	this->EventCallbackCommand->SetAbortFlag(1);
	this->StartInteraction();
	this->Interactor->Render();
}
	

void OnLeftButtonUp()
{
	if (m_mousedown) 	
	{
		m_mousedown=FALSE;
		this->EventCallbackCommand->SetAbortFlag(1);
		this->EndInteraction();
		this->InvlokeEven(vtkCommand::EndInteractionEvent,NULL);
		this->Interactor->Render
	}
	
	
}
	



virtual void OnMouseMove()
{
	if (!m_mousedown) return;
	
	int x=this->Interactor->GetEventPosition()[0];
	int y=this->Interactor->GetEventPosition()[1];
	
	double focalpoint[4],pickpoint[4];
	
	double z;
	
	vtkCamera *camera =m_renderer->GetActiveCamera();
	
	if (!camera) return;
	
	this->ComputeWorldToDisplay(m_lastpickposition[0],
		m_lastpickposition[1],m_lastpickposition[2],focalpoint);
	
	this->ComputeDisplayToWorld(double(x),double(y),z,pickpoint);
	
	float e1[3],e2[3];
	
	m_linesource->GetPoint1(e1);
	m_linesource->GetPoint1(e2);
	
	e1[0]+=pickpoint[0];
	e1[1]+=pickpoint[1];
	e1[2]+=pickpoint[2];
	
	e2[0]+=pickpoint[0];
	e2[1]+=pickpoint[1];
	e2[2]+=pickpoint[2];
	
	m_linesource->SetPoint1(e1);
	m_linesource->SetPoint2(e2);
	m_linesource->Update();
	
	this->EventCallbackCommand->SetAbortFlag(1);
	this->InvlokeEvent(vtkCommand::InteractionEvent,NULL);
	this->Interactor->Render();
}
	


};




void main()
{
vtkrenderer *rn=vtkrenderer::New();
vtkRenderWindow *win=vtkRenderWindow::New();
vtkRenderWindowObserver *intr=vtkRenderWindowObserver::New();

win->AddRenderer(rn);
intr->SetRenderWindow(win);

grids *cg;

cg=new grids(rn);
intr->Initialize();
rn->Render();
intr->Start();

delete cg;

}


More information about the vtkusers mailing list