Re: [wxVTK] wxAuiNotebook with wxVTK
Brought to you by:
malat
From: Sander N. <nie...@st...> - 2008-04-07 21:34:13
|
Dear Kerry, wxWidgets and VTK use different ways to delete objects. In this case you want to delete it yourself the VTK way which is using '...- >Delete()' and on the other hand a wx object is doing it the wxWidgets way and thinks it can call 'delete ...'. This generates a conflict. The delete will not decrese the VTK refcount and will result in your VTK error, but you can't really prevent the call to delete either (so doing your own Delete() will also give you trouble). I had this problem as well and got it solved differently: I was already using a derived class of wxVTKRenderWindowInteractor and for that class I added the following part to the destructor (at the end): ---- SetRenderWindow(NULL); SetInteractorStyle(NULL); // Normally one has to remove a wxVTKRenderWindowInteractor object using 'window->Delete()', // but we also would like 'delete window' to work (which is needed for e.g. Python wrapping) // This should be safe as long the reference count is 1 (i.e. no other objects have a reference to this object) // The problem is that we can't use Delete() or Unregister(NULL) here, because that would recursively invoke the // destructor. Therefore, we just explicitly decrease the reference count by 1 if it was 1 (this way you will still // get a warning from VTK if the reference count afterwards was not 0). if (GetReferenceCount() == 1) { SetReferenceCount(0); } --- This is the safest solution to this issue that I know of. Note that it also doesn't break a delete using the VTK-style Delete() mechanism (since it won't do anything special if the reference count is already 0, and the VTK Delete function will never call the destructor if the refcount was still higher than that). For your situation, you could probably adapt the wxVTKRenderWindowInteractor.cpp itself (instead of creating a subclass) and add the given routines to the (currently empty) wxVTKRenderWindowInteractor destructor. If this does indeed work, I think it might even be worth considering to add this reference count trick to the wxVTKRenderWindowInteractor destructor of the official wxVTKRenderWindowInteractor package. Mathieu? Best regards, Sander Niemeijer On 7 apr 2008, at 22:56, KR Loux wrote: > Hello, > > I am using a wxAuiNotebook which has a page that is a > wxVTKRenderWindowInteractor. Everything works fine until I either > close the page or exit the application, at which time the > application will crash or I get an error in the VTK debug window > about trying to delete an object with a non-zero reference count, > depending on how I handle deleting the wxVTKRenderWindowInteractor > (wxVTK from here on). For simplicity, I'll focus on just the case > of closing the application. If in the main frame's destructor, I do > nothing to remove the wxVTK object, I get the error about deleting > an object with a non-zero ref. count (makes sense). I added the > wxVTKObject->Delete(), which does reduce the reference count to > zero, but this causes a crash in wxObject (this is called from > wxAuiNotebook::DeletePage()): > > bool wxObject::IsKindOf(wxClassInfo *info) const > { > wxClassInfo *thisInfo = GetClassInfo();// Crashes on this line > return (thisInfo) ? thisInfo->IsKindOf(info) : false ; > } > > I think I am successfully deleting the wxVTK object with the - > >Delete() function, and then the notebook is trying to access it > again in it's destructor. Is this something anyone has experienced > before? Has anyone found a solution? > > I came up with a method that compiles and runs just fine, but I have > doubts about whether or not this is a good (safe?) approach. I have > not tested for memory leaks, but I suspect that if this method > doesn't work, that will be why. Below is the code for my main > frame's destructor: > > MainFrame::~MainFrame() > { > m_mgr.UnInit(); > > if (wxVTKObject) > { > wxVTKObject->Delete();// Delete the > wxVTKRenderWindowInteractor the proper way > wxWindow *Dummy = new wxWindow();// Create a dummy wxWindow > > // Assign the pointer for the wxVTKRenderWindowInteractor to > point to a wxWindow > // object instead. Now when the wxAuiNotebook is deleted, > this will point to > // something than can be deleted? > wxVTKObject = (wxVTKRenderWindowInteractor*)Dummy; > } > } > > Is this OK? Is there a better way? > > I'm using wxWidgets 2.8.7 and VTK 5.0 in MSW. > > > Thank you for your help, > > Kerry > ------------------------------------------------------------------------- > This SF.net email is sponsored by the 2008 JavaOne(SM) Conference > Register now and save $200. Hurry, offer ends at 11:59 p.m., > Monday, April 7! Use priority code J8TLD2. > http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone_______________________________________________ > Wxvtk-users mailing list > Wxv...@li... > https://lists.sourceforge.net/lists/listinfo/wxvtk-users |