[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 &quot;trying<BR>
> to delete object with non zero reference count&quot;<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-&gt;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>
> &nbsp; &nbsp; &nbsp;m_renderer=renderer;<BR>
> &nbsp; &nbsp; &nbsp;m_linesource=vtkLineSource::New();<BR>
> &nbsp; &nbsp; &nbsp;m_datamapper=vtkDataMapper::New();<BR>
> &nbsp; &nbsp; &nbsp;m_actor=vtkActor::New();<BR>
> &nbsp; &nbsp; &nbsp;m_picker=vtkCellPicker::New();<BR>
> &nbsp; &nbsp; &nbsp;m_linesource.Point1(0.0,0.0,0.0);<BR>
> &nbsp; &nbsp; &nbsp;m_linesource.Point1(0.0,10.0,0.0);<BR>
> &nbsp; &nbsp;
> &nbsp;m_datamapper-&gt;SetInput(linesource-&gt;GetOutput());<BR>
> &nbsp; &nbsp; &nbsp;m_actor-&gt;SetMapper(m_datamapper);<BR>
> &nbsp; &nbsp; &nbsp;m_picker-&gt;AddPickList(m_actor);<BR>
> &nbsp; &nbsp; &nbsp; &nbsp; m_renderer-&gt;AddActor(m_actor);<BR>
> &nbsp; &nbsp; &nbsp;m_renderer-&gt;Render();<BR>
> &nbsp; &nbsp;
> &nbsp;this-&gt;EventCallbackCommand-&gt;SetCallback(grids::ProcessEvents);<BR>
> <BR>
> }<BR>
> <BR>
> ~grids()<BR>
> {<BR>
> //this-&gt;ReferenceCount--;&nbsp;  //if i uncomment this line<BR>
> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &nbsp; &nbsp;  // then the program works fine<BR>
> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &nbsp; &nbsp;  //if i comment <BR>
> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &nbsp; &nbsp;  //this line the program displays a<BR>
> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &nbsp; &nbsp;  //message saying &quot;trying to delete<BR>
> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &nbsp; &nbsp; //an object with non zero refernce count <BR>
> <BR>
> &nbsp; &nbsp; &nbsp;m_actor-&gt;Delete();<BR>
> &nbsp; &nbsp; &nbsp;m_datamapper-&gt;Delete();<BR>
> &nbsp; &nbsp; &nbsp;m_linesource-&gt;Delete();<BR>
> &nbsp; &nbsp; &nbsp;m_picker-&gt;Delete();<BR>
> <BR>
> }<BR>
> <BR>
> static void ProcessEvents(vtkObject object,unsigned long event, void
> *clientdata,void *calldata)<BR>
> {<BR>
> <BR>
> grid *self=reinterpret_cast&lt;grid *&gt;(clientdata);<BR>
> <BR>
> switch(event)<BR>
> {<BR>
> &nbsp; &nbsp; &nbsp;case vtkCommand::LeftButtonPressEvent:<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;self-&gt;OnLeftButtonDown();<BR>
> &nbsp; &nbsp; &nbsp;break;<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;case vtkCommand::LeftButtonReleaseEvent:<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;self-&gt;OnLeftButtonUp();<BR>
> &nbsp; &nbsp; &nbsp;break;<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;case vtkCommand::MouseMoveEvent:<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;self-&gt;OnMouseMove();<BR>
> &nbsp; &nbsp; &nbsp;break;<BR>
> }<BR>
> }<BR>
> <BR>
> void SetEnabled(int enabling)<BR>
> {<BR>
> &nbsp; &nbsp; &nbsp;if (!this-&gt;Interactor) return;<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;if (enabling)<BR>
> &nbsp; &nbsp; &nbsp;{<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;if (this-&gt;Enabled) return;<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;this-&gt;Enabled=1;<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
> &nbsp;this-&gt;CurrentRenderer=m_renderer;<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;vtkRenderWindowInteractor
> *i=this-&gt;Interactor;<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
> &nbsp;i-&gt;AddObserver(vtkCommand::MouseMoveEvent,<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
> &nbsp;this-&gt;EventCallbackCommand,this-&gt;priority);<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
> &nbsp;i-&gt;AddObserver(vtkCommand::LeftButtonPressEvent,<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
> &nbsp;this-&gt;EventCallbackCommand,this-&gt;priority);<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
> &nbsp;i-&gt;AddObserver(vtkCommand::LeftButtonReleaseEvent,<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
> &nbsp;this-&gt;EventCallbackCommand,this-&gt;priority);<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
> &nbsp;this-&gt;InvokeEvent(vtkCommand::EnableEvent,NULL);<BR>
> &nbsp; &nbsp; &nbsp;}<BR>
> &nbsp; &nbsp; &nbsp;else<BR>
> &nbsp; &nbsp; &nbsp;{<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;if (!this-&gt;Enabled) return;<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;this-&gt;Enabled=0;<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;this-&gt;CurrentRenderer=NULL;<BR>
> &nbsp; &nbsp;
> &nbsp;this-&gt;Interactor-&gt;RemoveObserver(this-&gt;EventCallbackCommand);<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
> &nbsp;this-&gt;InvokeEvent(vtkCommand::DisableEvent,NULL);<BR>
> &nbsp; &nbsp; &nbsp;}<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;this-&gt;Interactor-&gt;Render();<BR>
> }<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> <BR>
> void OnLeftButtonDown()<BR>
> {<BR>
> &nbsp; &nbsp; &nbsp;int x=this-&gt;Interactor-&gt;GetEventPosition()[0];<BR>
> &nbsp; &nbsp; &nbsp;int y=this-&gt;Interactor-&gt;GetEventPosition()[1];<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;vtkAssemblyPath *path;<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp;
> &nbsp;this-&gt;m_picker-&gt;Pick(x,y,0.0,this-&gt;m_renderer);<BR>
> &nbsp; &nbsp; &nbsp;path=this-&gt;m_picker-&gt;GetPath();<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;if (path!=NULL)<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;{<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
> &nbsp;m_picker-&gt;GetPickPosition(m_lastpickposition);<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;m_mousedown=TRUE;<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
> &nbsp;this-&gt;InvokeEvent(vtkCommand::StartInteractionEvent,NULL);<BR>
> &nbsp; &nbsp; &nbsp;}<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;else <BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;return;<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;this-&gt;EventCallbackCommand-&gt;SetAbortFlag(1);<BR>
> &nbsp; &nbsp; &nbsp;this-&gt;StartInteraction();<BR>
> &nbsp; &nbsp; &nbsp;this-&gt;Interactor-&gt;Render();<BR>
> }<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> <BR>
> void OnLeftButtonUp()<BR>
> {<BR>
> &nbsp; &nbsp; &nbsp;if (m_mousedown) &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;{<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;m_mousedown=FALSE;<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
> &nbsp;this-&gt;EventCallbackCommand-&gt;SetAbortFlag(1);<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;this-&gt;EndInteraction();<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
> &nbsp;this-&gt;InvlokeEven(vtkCommand::EndInteractionEvent,NULL);<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;this-&gt;Interactor-&gt;Render<BR>
> &nbsp; &nbsp; &nbsp;}<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> }<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> <BR>
> <BR>
> <BR>
> virtual void OnMouseMove()<BR>
> {<BR>
> &nbsp; &nbsp; &nbsp;if (!m_mousedown) return;<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;int x=this-&gt;Interactor-&gt;GetEventPosition()[0];<BR>
> &nbsp; &nbsp; &nbsp;int y=this-&gt;Interactor-&gt;GetEventPosition()[1];<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;double focalpoint[4],pickpoint[4];<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;double z;<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;vtkCamera *camera =m_renderer-&gt;GetActiveCamera();<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;if (!camera) return;<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp;
> &nbsp;this-&gt;ComputeWorldToDisplay(m_lastpickposition[0],<BR>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
> &nbsp;m_lastpickposition[1],m_lastpickposition[2],focalpoint);<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp;
> &nbsp;this-&gt;ComputeDisplayToWorld(double(x),double(y),z,pickpoint);<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;float e1[3],e2[3];<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;m_linesource-&gt;GetPoint1(e1);<BR>
> &nbsp; &nbsp; &nbsp;m_linesource-&gt;GetPoint1(e2);<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;e1[0]+=pickpoint[0];<BR>
> &nbsp; &nbsp; &nbsp;e1[1]+=pickpoint[1];<BR>
> &nbsp; &nbsp; &nbsp;e1[2]+=pickpoint[2];<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;e2[0]+=pickpoint[0];<BR>
> &nbsp; &nbsp; &nbsp;e2[1]+=pickpoint[1];<BR>
> &nbsp; &nbsp; &nbsp;e2[2]+=pickpoint[2];<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;m_linesource-&gt;SetPoint1(e1);<BR>
> &nbsp; &nbsp; &nbsp;m_linesource-&gt;SetPoint2(e2);<BR>
> &nbsp; &nbsp; &nbsp;m_linesource-&gt;Update();<BR>
> &nbsp; &nbsp; &nbsp;<BR>
> &nbsp; &nbsp; &nbsp;this-&gt;EventCallbackCommand-&gt;SetAbortFlag(1);<BR>
> &nbsp; &nbsp;
> &nbsp;this-&gt;InvlokeEvent(vtkCommand::InteractionEvent,NULL);<BR>
> &nbsp; &nbsp; &nbsp;this-&gt;Interactor-&gt;Render();<BR>
> }<BR>
> &nbsp; &nbsp; &nbsp;<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-&gt;AddRenderer(rn);<BR>
> intr-&gt;SetRenderWindow(win);<BR>
> <BR>
> grids *cg;<BR>
> <BR>
> cg=new grids(rn);<BR>
> intr-&gt;Initialize();<BR>
> rn-&gt;Render();<BR>
> intr-&gt;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