[vtkusers] please help me, iam getting a message saying "trying to delete a object with non zero reference coun
Luke J West
ljw at soc.soton.ac.uk
Fri Jan 23 07:26:56 EST 2004
Quoting david michell <davidmichell at rediffmail.com>:
Does VC++ .NET *really* allow you to compile this code?
The code you posted has the following line..
vtkrenderer *rn=vtkrenderer::New();
lower case 'r'? Are you sure?
luke
> <P>
> hello everybody,<BR>
> thanks for the all the help and wonderful code.<BR>
> <BR>
> iam writing a class to implement moving a line.<BR>
> <BR>
> i basically ripped the code from the linewidget.<BR>
> <BR>
> i have one problem, when I destroy the object in the main() by using delete,
> iam getting a message saying "trying<BR>
> to delete object with non zero reference count"<BR>
> <BR>
> i traced through the program and found out that the object was clientdata,
> from the <BR>
> EventCallbackCommand.<BR>
> <BR>
> i dont know what to do with this information.<BR>
> <BR>
> what should i do to the code.<BR>
> <BR>
> also if i do this->ReferenceCount-- in the destructor, then the program
> works correctly.<BR>
> <BR>
> mine is vtk42 and VC++ 7 .net enabled<BR>
> <BR>
> please help me.<BR>
> <BR>
> thanking you,<BR>
> david michell<BR>
> <BR>
> <BR>
> <BR>
> //all the header files go here<BR>
> class grids : public vtkInteractorObserver<BR>
> {<BR>
> BOOL m_mousedown;<BR>
> vtkRenderer *m_renderer;<BR>
> vtkLineSource *m_linesource;<BR>
> vtkPolyDataMapper *m_datamapper;<BR>
> vtkActor *m_actor;<BR>
> float m_lastpickposition[4];<BR>
> <BR>
> public:<BR>
> grids(vtkRenderer *renderer)<BR>
> {<BR>
> m_renderer=renderer;<BR>
> m_linesource=vtkLineSource::New();<BR>
> m_datamapper=vtkDataMapper::New();<BR>
> m_actor=vtkActor::New();<BR>
> m_picker=vtkCellPicker::New();<BR>
> m_linesource.Point1(0.0,0.0,0.0);<BR>
> m_linesource.Point1(0.0,10.0,0.0);<BR>
>
> m_datamapper->SetInput(linesource->GetOutput());<BR>
> m_actor->SetMapper(m_datamapper);<BR>
> m_picker->AddPickList(m_actor);<BR>
> m_renderer->AddActor(m_actor);<BR>
> m_renderer->Render();<BR>
>
> this->EventCallbackCommand->SetCallback(grids::ProcessEvents);<BR>
> <BR>
> }<BR>
> <BR>
> ~grids()<BR>
> {<BR>
> //this->ReferenceCount--; //if i uncomment this line<BR>
>
> // then the program works fine<BR>
>
> //if i comment <BR>
>
> //this line the program displays a<BR>
>
> //message saying "trying to delete<BR>
>
> //an object with non zero refernce count <BR>
> <BR>
> m_actor->Delete();<BR>
> m_datamapper->Delete();<BR>
> m_linesource->Delete();<BR>
> m_picker->Delete();<BR>
> <BR>
> }<BR>
> <BR>
> static void ProcessEvents(vtkObject object,unsigned long event, void
> *clientdata,void *calldata)<BR>
> {<BR>
> <BR>
> grid *self=reinterpret_cast<grid *>(clientdata);<BR>
> <BR>
> switch(event)<BR>
> {<BR>
> case vtkCommand::LeftButtonPressEvent:<BR>
> self->OnLeftButtonDown();<BR>
> break;<BR>
> <BR>
> case vtkCommand::LeftButtonReleaseEvent:<BR>
> self->OnLeftButtonUp();<BR>
> break;<BR>
> <BR>
> case vtkCommand::MouseMoveEvent:<BR>
> self->OnMouseMove();<BR>
> break;<BR>
> }<BR>
> }<BR>
> <BR>
> void SetEnabled(int enabling)<BR>
> {<BR>
> if (!this->Interactor) return;<BR>
> <BR>
> if (enabling)<BR>
> {<BR>
> if (this->Enabled) return;<BR>
> this->Enabled=1;<BR>
>
> this->CurrentRenderer=m_renderer;<BR>
> <BR>
> vtkRenderWindowInteractor
> *i=this->Interactor;<BR>
>
> i->AddObserver(vtkCommand::MouseMoveEvent,<BR>
>
> this->EventCallbackCommand,this->priority);<BR>
>
> i->AddObserver(vtkCommand::LeftButtonPressEvent,<BR>
>
> this->EventCallbackCommand,this->priority);<BR>
>
> i->AddObserver(vtkCommand::LeftButtonReleaseEvent,<BR>
>
> this->EventCallbackCommand,this->priority);<BR>
> <BR>
>
> this->InvokeEvent(vtkCommand::EnableEvent,NULL);<BR>
> }<BR>
> else<BR>
> {<BR>
> if (!this->Enabled) return;<BR>
> this->Enabled=0;<BR>
> this->CurrentRenderer=NULL;<BR>
>
> this->Interactor->RemoveObserver(this->EventCallbackCommand);<BR>
>
> this->InvokeEvent(vtkCommand::DisableEvent,NULL);<BR>
> }<BR>
> <BR>
> this->Interactor->Render();<BR>
> }<BR>
> <BR>
> <BR>
> <BR>
> <BR>
> void OnLeftButtonDown()<BR>
> {<BR>
> int x=this->Interactor->GetEventPosition()[0];<BR>
> int y=this->Interactor->GetEventPosition()[1];<BR>
> <BR>
> vtkAssemblyPath *path;<BR>
> <BR>
>
> this->m_picker->Pick(x,y,0.0,this->m_renderer);<BR>
> path=this->m_picker->GetPath();<BR>
> <BR>
> if (path!=NULL)<BR>
> <BR>
> {<BR>
>
> m_picker->GetPickPosition(m_lastpickposition);<BR>
> m_mousedown=TRUE;<BR>
>
> this->InvokeEvent(vtkCommand::StartInteractionEvent,NULL);<BR>
> }<BR>
> <BR>
> else <BR>
> return;<BR>
> <BR>
> this->EventCallbackCommand->SetAbortFlag(1);<BR>
> this->StartInteraction();<BR>
> this->Interactor->Render();<BR>
> }<BR>
> <BR>
> <BR>
> void OnLeftButtonUp()<BR>
> {<BR>
> if (m_mousedown) <BR>
> {<BR>
> m_mousedown=FALSE;<BR>
>
> this->EventCallbackCommand->SetAbortFlag(1);<BR>
> this->EndInteraction();<BR>
>
> this->InvlokeEven(vtkCommand::EndInteractionEvent,NULL);<BR>
> this->Interactor->Render<BR>
> }<BR>
> <BR>
> <BR>
> }<BR>
> <BR>
> <BR>
> <BR>
> <BR>
> virtual void OnMouseMove()<BR>
> {<BR>
> if (!m_mousedown) return;<BR>
> <BR>
> int x=this->Interactor->GetEventPosition()[0];<BR>
> int y=this->Interactor->GetEventPosition()[1];<BR>
> <BR>
> double focalpoint[4],pickpoint[4];<BR>
> <BR>
> double z;<BR>
> <BR>
> vtkCamera *camera =m_renderer->GetActiveCamera();<BR>
> <BR>
> if (!camera) return;<BR>
> <BR>
>
> this->ComputeWorldToDisplay(m_lastpickposition[0],<BR>
>
> m_lastpickposition[1],m_lastpickposition[2],focalpoint);<BR>
> <BR>
>
> this->ComputeDisplayToWorld(double(x),double(y),z,pickpoint);<BR>
> <BR>
> float e1[3],e2[3];<BR>
> <BR>
> m_linesource->GetPoint1(e1);<BR>
> m_linesource->GetPoint1(e2);<BR>
> <BR>
> e1[0]+=pickpoint[0];<BR>
> e1[1]+=pickpoint[1];<BR>
> e1[2]+=pickpoint[2];<BR>
> <BR>
> e2[0]+=pickpoint[0];<BR>
> e2[1]+=pickpoint[1];<BR>
> e2[2]+=pickpoint[2];<BR>
> <BR>
> m_linesource->SetPoint1(e1);<BR>
> m_linesource->SetPoint2(e2);<BR>
> m_linesource->Update();<BR>
> <BR>
> this->EventCallbackCommand->SetAbortFlag(1);<BR>
>
> this->InvlokeEvent(vtkCommand::InteractionEvent,NULL);<BR>
> this->Interactor->Render();<BR>
> }<BR>
> <BR>
> <BR>
> <BR>
> };<BR>
> <BR>
> <BR>
> <BR>
> <BR>
> void main()<BR>
> {<BR>
> vtkrenderer *rn=vtkrenderer::New();<BR>
> vtkRenderWindow *win=vtkRenderWindow::New();<BR>
> vtkRenderWindowObserver *intr=vtkRenderWindowObserver::New();<BR>
> <BR>
> win->AddRenderer(rn);<BR>
> intr->SetRenderWindow(win);<BR>
> <BR>
> grids *cg;<BR>
> <BR>
> cg=new grids(rn);<BR>
> intr->Initialize();<BR>
> rn->Render();<BR>
> intr->Start();<BR>
> <BR>
> delete cg;<BR>
> <BR>
> }
> </P>
> <br><br>
> <A target="_blank"
> HREF="http://clients.rediff.com/signature/track_sig.asp"><IMG
>
SRC="http://ads.rediff.com/RealMedia/ads/adstream_nx.cgi/www.rediffmail.com/inbox.htm@Bottom"
> BORDER=0 VSPACE=0 HSPACE=0 HEIGHT=74 WIDTH=496></a>
>
Luke J West : Research Assistant : e-Science
--------------------------------------------
Rm. 566/12, School of Ocean & Earth Sciences
Southampton Oceanography Centre, Southampton
SO14 3ZH United Kingdom
--------------------------------------------
Tel: +44 23 8059 4801 Fax: +44 23 8059 3052
Mob: +44 79 6107 4783
More information about the vtkusers
mailing list