使用对象的浅拷贝时出现双重释放错误...如何修复?

发布于 2024-10-21 12:43:44 字数 616 浏览 2 评论 0原文

如何从浅复制对象与原始对象中删除双重(删除)错误。

一个简单的例子:

class INT
{
   int *p; //dynamic.

   //define here fancy constructors etc.
   set(int i){ p=new int; p=i;}
   ~INT();
}

INT::~INT()
{
   if(p) delete p;
}

void somefunction(INT a)
{
   //done some stuff ,e.g. call some display function
}
//note here, that a destructor will be called, and a.p will vanish.

int main(void)
{
   INT a; a.set(2);
   somefunction(a);//shallow copy
}
//CRASH BOOM BOOM!

我想要一个通用的解决方案,因为传递对象是一件微不足道的事情,而像这样天真的事情,导致可怕/可怕的错误只是“太棒了”。

我怀疑有很多方法可以解决这个问题(其中一些我什至能想到),但我很好奇是否有任何通用的(几乎所有地方都适用)方法来解决这个问题?

How do I remove double (delete) errors from shallow copied object vs original object.

A simple example:

class INT
{
   int *p; //dynamic.

   //define here fancy constructors etc.
   set(int i){ p=new int; p=i;}
   ~INT();
}

INT::~INT()
{
   if(p) delete p;
}

void somefunction(INT a)
{
   //done some stuff ,e.g. call some display function
}
//note here, that a destructor will be called, and a.p will vanish.

int main(void)
{
   INT a; a.set(2);
   somefunction(a);//shallow copy
}
//CRASH BOOM BOOM!

I would like a generic solution, because passing objects is a trivial thing, and something as naive as this, resulting into a horrific/terrific error is just 'awesome'.

I suspect there are many ways around this (some of which even I can think of), but I was curious if there is any generic (applicable almost everywhere) way of solving this problem?

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

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

发布评论

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

评论(4

忆依然 2024-10-28 12:43:44

每当类中有指针对象时,您就需要声明自己的复制构造函数和赋值运算符方法以避免浅复制问题。
查看此链接以获取更多信息

Whenever you have apointer object inside your class then you need to declare your own copy constructor and assignment operator method to avoid shallow copy problem.
Look ate this link for mor info on this

流殇 2024-10-28 12:43:44

对于所有具有动态成员的对象,通常最好定义自己的复制和复制分配操作,正是出于这个原因。

关于“大”对象和昂贵的复制,引用计数是一种采用的技术通过许多语言和模式来规避指针所有权问题。请注意,在这种情况下,对象的所有副本都指向同一个共享对象,因此一个实例对共享对象的修改将对其他实例可见。请参阅 boost shared_ptr 文档以获取更多信息。

For all objects with dynamic members, it is usually best to define your own copy and copy-assignment operations for just this reason.

Regarding 'big' objects and expensive copying, reference counting is a technique employed by many languages and patterns to circumvent this problem of pointer ownership. Note that all copies of an object point to the same shared object in this case, so a modification of the shared object from one instance will be visible to others. See the boost shared_ptr documentation for more info.

夏见 2024-10-28 12:43:44

病人:医生,我这样做的时候很痛!
医生:别这样做。

您可以在不处理原始指针的情况下很好地编写代码的可能性至少为 100:1。根据具体情况,您可能需要一个智能指针,或者您可能需要一个集合,但您需要指针的机会似乎很小。

您最好告诉我们您想要完成什么,而不是询问如何解决您正在做的事情,这样我们就可以告诉您如何做到这一点。

Patient: Doctor, it hurts when I do this!
Doctor: Don't do that.

Chances are at least 100:1 that you can write your code just fine without dealing with a raw pointer. Depending on the situation, you may need a smart pointer or you may need a collection, but the chances that you need a pointer seem quite remote.

Instead of asking about how to fix what you're doing, you'd be much better off telling us what you're trying to accomplish, so we can tell you about how to do that.

不可一世的女人 2024-10-28 12:43:44

问题“如何避免浅复制对象的双重删除?”实际上应该是“当我不想要浅拷贝时,如何避免浅拷贝?”。

一些选项是:
- 避免指向已分配内存的原始指针
- 不要复制对象! :-)
- 管理对象外部的内存
- 使用智能指针
- 实现深度复制

您的选择取决于您实际使用对象的目的。

The question "How do I avoid double delete from shallow copied objects?" should really be "How do I avoid shallow copies, when I don't want that?".

Some options are:
- avoid raw pointers to allocated memory
- don't copy the objects! :-)
- manage the memory outside of the objects
- use a smart pointer
- implement deep copying

Your choice depends on what you are actually using your objects for.

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