开发者

Reference counting in wxWidgets, how does it work in this simple case?

开发者 https://www.devze.com 2023-01-25 06:16 出处:网络
My program is crashing and I\'m having a hard time wrapping my head around why it crashes. The main problem is that the wxwidgets documentation says that reference counting is used for wxImage objects

My program is crashing and I'm having a hard time wrapping my head around why it crashes. The main problem is that the wxwidgets documentation says that reference counting is used for wxImage objects. What exactly does this mean? Why does this code crash it?

wxImage* t = m_default_image; //m_default_image is a pointer pointing to a valid wxImage object.

wxDELETE(m_default_image);
if(t->IsOk())
{
    wxLogMessage("reference counting works!");
}

The line that crashes is the t->IsOK(). Shouldn't the reference counting p开发者_JAVA技巧revent the actual object from being deleted so my t ptr still points to something valid?


Let me explain what is happening:

  1. You create a pointer to a wxImage that you have already created somewhere.
  2. You then delete it.
  3. You then attempt to dereference a deleted pointer and call 'IsOk()' on the resulting object, which doesn't exist anymore, because of the previous step.

Step 2 may or may not have actually deleted the object, but it likely deleted the reference as well. Depending on how wx implemented reference counting, and how you've managed references, the wxImage object may have a reference count of one at the start of your routine - therefore, wxDELETE will reduce the reference count to zero, and thus delete the object as well.

There is no way that wx could automatically manage your objects using reference counts if you are using pointers in the way you are. Perhaps you want to use the copy constructor, and stop using manual memory handling? C++ has the RAII technique - use it.

Lastly, can I ask why m_default_image is a pointer? You're just making life hard for yourself by using raw pointers all the time.


To add to Arafangion's answer (and I would just comment if I had enough rep points to do so), if you read the documentation for wxDELETE(), you would have seen that it actually says:

This function uses operator delete to free the pointer and also sets it to NULL.

C++ pointers don't have anything to do with the implementation of reference counting in wxWidgets (and I don't think it does anywhere else except when using smart pointers), and as wxDELETE is meant to be used with pointers, it also has absolutely nothing to do with reference counting except that it deleted an object that may or may not have been reference counted, and if it was, and the objects reference count was more than 1, the object really wasn't deleted, however, your pointer was still invalidated.


Reference counting is used internally by wxImage objects meaning that you can copy them cheaply as the underlying image data is shared. It is not exposed in wx API and definitely doesn't have anything to do with wxImage pointers.

0

精彩评论

暂无评论...
验证码 换一张
取 消