wxWidgets 中的引用计数,在这个简单的情况下它是如何工作的?

发布于 2024-10-02 14:34:22 字数 369 浏览 2 评论 0原文

我的程序崩溃了,我很难理解它崩溃的原因。主要问题是wxwidgets文档说引用计数用于wxImage对象。这究竟意味着什么?为什么这段代码会崩溃?

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!");
}

崩溃的那行是 t->IsOK()。难道引用计数不应该阻止实际对象被删除,这样我的 t ptr 仍然指向有效的东西吗?

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 prevent the actual object from being deleted so my t ptr still points to something valid?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

顾忌 2024-10-09 14:34:23

要添加 Arafangion 的答案(如果我有足够的代表点,我只会发表评论),如果您阅读 wxDELETE() 的文档,您会看到它实际上说:

该函数使用运算符delete 来释放指针并将其设置为NULL。

C++ 指针与 wxWidgets 中引用计数的实现没有任何关系(我认为除了使用智能指针之外,它在其他任何地方都没有作用),并且由于 wxDELETE 旨在与指针一起使用,因此它也绝对有与引用计数无关,只是它删除了一个可能已或未进行引用计数的对象,如果是,并且对象引用计数大于 1,则该对象确实没有被删除,但是,您的指针是仍然无效。

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.

筱武穆 2024-10-09 14:34:23

引用计数由 wxImage 对象 在内部使用,这意味着您可以在共享底层图像数据时廉价地复制它们。它没有在 wx API 中公开,并且绝对与 wxImage 指针 没有任何关系。

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.

一个人的旅程 2024-10-09 14:34:22

让我解释一下发生了什么:

  1. 您创建了一个指向已在某处创建的 wxImage 的指针。
  2. 然后您将其删除。
  3. 然后,您尝试取消引用已删除的指针并对生成的对象调用“IsOk()”,由于上一步,该对象不再存在。

步骤 2 可能会也可能不会实际删除该对象,但它可能也删除了引用。根据 wx 实现引用计数的方式以及管理引用的方式,wxImage 对象在例程开始时的引用计数可能为 1 - 因此,wxDELETE 会将引用计数减少到零,从而删除该对象:出色地。

如果您按照现在的方式使用指针,那么 wx 就无法使用引用计数自动管理您的对象。也许您想使用复制构造函数,并停止使用手动内存处理? C++ 有 RAII 技术 - 使用它。

最后,我能问一下为什么m_default_image是一个指针吗?一直使用原始指针只会让自己的生活变得艰难。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文