Re [vtkusers] please help

Luke J West ljw at soc.soton.ac.uk
Fri Jan 23 12:40:34 EST 2004


The short answer...

 change the line that reads

 delete cg;

 for this...

 cg->Delete();


The slightly longer answer is that you shouldn't really be using New() either.
You have derived from a vtk class, therefore you should declare the default
constructor protected and provide create through a static New() method somethin
like this. Then your code will have nice New and Delete pairs.



class grids: public vtkInteractorObserver {
 //.....
 vtkRenderer* ren;

protected:
 grids():ren(0) {}

 ~grids() {if (ren) ren->Delete();}

public:
 static grids* New() {return new grids;}
 //...

 void Initialize(vtkRenderer* r) {
  if (ren) ren->Delete();

  ren=r->Register(this)
 }
}; // grids


hopefully no mistakes in the above..


Quoting david  michell <davidmichell at rediffmail.com>:

> <P>
> iam removing all the tabs from my code for clarity on mail client<BR>
> <BR>
> <BR>
> 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>
> class grids : public vtkInteractorObserer<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-&gt;SetInput(linesource-&gt;GetOutput());<BR>
> m_actor-&gt;SetMapper(m_datamapper);<BR>
> m_picker-&gt;AddPickList(m_actor);<BR>
> m_render-&gt;Render();<BR>
> this-&gt;EventCallbackCommand-&gt;SetCallback(grids::ProcessEvents);<BR>
> <BR>
> }<BR>
> <BR>
> ~grids()<BR>
> {<BR>
> //this-&gt;ReferenceCount--;&nbsp; //if i uncomment this line the program
> terminates normally, without<BR>
> //this line the program displays a window saying, trying to delete<BR>
> //object with non zero reference count&nbsp; &nbsp; &nbsp;<BR>
> <BR>
> m_actor-&gt;Delete();<BR>
> m_datamapper-&gt;Delete();<BR>
> m_linesource-&gt;Delete();<BR>
> 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>
> case vtkCommand::LeftButtonPressEvent:<BR>
> self-&gt;OnLeftButtonDown();<BR>
> break;<BR>
> <BR>
> case vtkCommand::LeftButtonReleaseEvent:<BR>
> self-&gt;OnLeftButtonUp();<BR>
> break;<BR>
> <BR>
> case vtkCommand::MouseMoveEvent:<BR>
> self-&gt;OnMouseMove();<BR>
> break;<BR>
> }<BR>
> }<BR>
> <BR>
> void SetEnabled(int enabling)<BR>
> {<BR>
> if (!this-&gt;Interactor) return;<BR>
> <BR>
> if (enabling)<BR>
> {<BR>
> if (this-&gt;Enabled) return;<BR>
> this-&gt;Enabled=1;<BR>
> this-&gt;CurrentRenderer=m_renderer;<BR>
> <BR>
> vtkRenderWindowInteractor *i=this-&gt;Interactor;<BR>
> i-&gt;AddObserver(vtkCommand::MouseMoveEvent,<BR>
> this-&gt;EventCallbackCommand,this-&gt;priority);<BR>
> i-&gt;AddObserver(vtkCommand::LeftButtonPressEvent,<BR>
> this-&gt;EventCallbackCommand,this-&gt;priority);<BR>
> i-&gt;AddObserver(vtkCommand::LeftButtonReleaseEvent,<BR>
> this-&gt;EventCallbackCommand,this-&gt;priority);<BR>
> <BR>
> this-&gt;InvokeEvent(vtkCommand::EnableEvent,NULL);<BR>
> }<BR>
> else<BR>
> {<BR>
> if (!this-&gt;Enabled) return;<BR>
> this-&gt;Enabled=0;<BR>
> this-&gt;CurrentRenderer=NULL;<BR>
> this-&gt;Interactor-&gt;RemoveObserver(this-&gt;EventCallbackCommand);<BR>
> this-&gt;InvokeEvent(vtkCommand::DisableEvent,NULL);<BR>
> }<BR>
> <BR>
> this-&gt;Interactor-&gt;Render();<BR>
> }<BR>
> <BR>
> <BR>
> <BR>
> <BR>
> void OnLeftButtonDown()<BR>
> {<BR>
> int x=this-&gt;Interactor-&gt;GetEventPosition()[0];<BR>
> int y=this-&gt;Interactor-&gt;GetEventPosition()[1];<BR>
> <BR>
> vtkAssemblyPath *path;<BR>
> <BR>
> this-&gt;m_picker-&gt;Pick(x,y,0.0,this-&gt;m_renderer);<BR>
> path=this-&gt;m_picker-&gt;GetPath();<BR>
> <BR>
> if (path!=NULL)<BR>
> <BR>
> {<BR>
> m_picker-&gt;GetPickPosition(m_lastpickposition);<BR>
> m_mousedown=TRUE;<BR>
> this-&gt;InvokeEvent(vtkCommand::StartInteractionEvent,NULL);<BR>
> }<BR>
> <BR>
> else <BR>
> return;<BR>
> <BR>
> this-&gt;EventCallbackCommand-&gt;SetAbortFlag(1);<BR>
> this-&gt;StartInteraction();<BR>
> this-&gt;Interactor-&gt;Render();<BR>
> }<BR>
> <BR>
> <BR>
> void OnLeftButtonUp()<BR>
> {<BR>
> if (m_mousedown) &nbsp; &nbsp; &nbsp;<BR>
> {<BR>
> m_mousedown=FALSE;<BR>
> this-&gt;EventCallbackCommand-&gt;SetAbortFlag(1);<BR>
> this-&gt;EndInteraction();<BR>
> this-&gt;InvlokeEvent(vtkCommand::EndInteractionEvent,NULL);<BR>
> this-&gt;Interactor-&gt;Render<BR>
> }<BR>
> <BR>
> <BR>
> }<BR>
> <BR>
> <BR>
> <BR>
> <BR>
> virtual void OnMouseMove()<BR>
> {<BR>
> if (!m_mousedown) return;<BR>
> <BR>
> int x=this-&gt;Interactor-&gt;GetEventPosition()[0];<BR>
> int y=this-&gt;Interactor-&gt;GetEventPosition()[1];<BR>
> <BR>
> double focalpoint[4],pickpoint[4];<BR>
> <BR>
> double z;<BR>
> <BR>
> vtkCamera *camera =m_renderer-&gt;GetActiveCamera();<BR>
> <BR>
> if (!camera) return;<BR>
> <BR>
> this-&gt;ComputeWorldToDisplay(m_lastpickposition[0],<BR>
> m_lastpickposition[1],m_lastpickposition[2],focalpoint);<BR>
> <BR>
> this-&gt;ComputeDisplayToWorld(double(x),double(y),z,pickpoint);<BR>
> <BR>
> float e1[3],e2[3];<BR>
> <BR>
> m_linesource-&gt;GetPoint1(e1);<BR>
> m_linesource-&gt;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-&gt;SetPoint1(e1);<BR>
> m_linesource-&gt;SetPoint2(e2);<BR>
> m_linesource-&gt;Update();<BR>
> <BR>
> this-&gt;EventCallbackCommand-&gt;SetAbortFlag(1);<BR>
> this-&gt;InvlokeEvent(vtkCommand::InteractionEvent,NULL);<BR>
> this-&gt;Interactor-&gt;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-&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>
> }<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