如何删除已取消引用的对象?

发布于 2024-11-30 04:46:16 字数 876 浏览 0 评论 0原文

我有以下代码

MyObject * func1() {
    MyObject * obj = new MyObject();
    // lots of stuff here
    return obj;
}

MyObject func2() {
    MyObject * obj = func1();
    // even more stuff here
    return *obj;
}

void main() {
    MyObject obj = func2()
}

,正如我从 得到的这里这段代码正在泄漏。这会:

MyObject * func1() {
    MyObject * obj = new MyObject();
    // lots of stuff here
    return obj;
}

MyObject func2() {
    MyObject * obj = func1();
    // even more stuff here
    MyObject obj_r(*obj);
    delete obj;
    return obj_r;
}

void main() {
    MyObject obj = func2()
}

解决问题吗?或者还有其他一些不错的解决方案吗?

在 b4 中:不,我不能从一开始就引用它,因为 func1() 在某些情况下返回 NULL。

upd:添加了一些评论,这样人们就不会认为我非常愚蠢

I have the following code

MyObject * func1() {
    MyObject * obj = new MyObject();
    // lots of stuff here
    return obj;
}

MyObject func2() {
    MyObject * obj = func1();
    // even more stuff here
    return *obj;
}

void main() {
    MyObject obj = func2()
}

As I got it from here this code is leaking. Will this:

MyObject * func1() {
    MyObject * obj = new MyObject();
    // lots of stuff here
    return obj;
}

MyObject func2() {
    MyObject * obj = func1();
    // even more stuff here
    MyObject obj_r(*obj);
    delete obj;
    return obj_r;
}

void main() {
    MyObject obj = func2()
}

resolve the issue? Or is there some other nice solutions?

in b4: no, I can't make it reference from the beginning, as func1() returns NULL in some cases.

upd: added some comments so that people didn't think I'm royally stupid

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

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

发布评论

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

评论(7

樱&纷飞 2024-12-07 04:46:16

一个更优雅的解决方案(并且更“正确”)是使用智能
指针:(

MyObject func2()
{
    return *std::auto_ptr<MyObject>(func1());
}

使用更现代的编译器,请使用 std::unique_ptr。或者如果您使用
Boost,你也可以使用 boost::scoped_ptr。)

我说得更“正确”,因为如果 MyObject 的复制构造函数
抛出异常,该解决方案仍然会删除该对象,其中
因为你的会泄漏。

A more elegant solution (and more "correct") would be to use a smart
pointer:

MyObject func2()
{
    return *std::auto_ptr<MyObject>(func1());
}

(With a more modern compiler, use std::unique_ptr. Or if you're using
Boost, you can also use boost::scoped_ptr.)

I say more "correct", because if the copy constructor of MyObject
throws an exception, this solution will still delete the object, where
as yours would leak.

年华零落成诗 2024-12-07 04:46:16

是的,这将解决内存泄漏问题。

一般来说,这不是一个很好的模式。但我不确定你想在这里实现什么目标!

Yes, that will resolve the memory leak.

This is not a very nice pattern, in general. But then I'm not sure what you're trying to achieve here!

烦人精 2024-12-07 04:46:16

避免内存泄漏的最佳方法是使用智能指针:

#include <memory>

MyObject func2() {
    std::unique_ptr<MyObject> obj(func1());
    // stuff here
    return *obj;
}

int main() {
    MyObject obj = func2();
}

这与您的解决方案几乎相同,但修复了在复制对象或执行“操作”时引发异常时您的内存泄漏问题。如果您不使用 C++11,请使用 auto_ptr 而不是 unique_ptr

The best way to avoid a memory leak is to use a smart pointer:

#include <memory>

MyObject func2() {
    std::unique_ptr<MyObject> obj(func1());
    // stuff here
    return *obj;
}

int main() {
    MyObject obj = func2();
}

This is almost the same as your solution, but fixes the memory leak that yours has if an exception is thrown while copying the object or doing the "stuff". If you're not using C++11, then use auto_ptr rather than unique_ptr.

七秒鱼° 2024-12-07 04:46:16

只需将 func2() 更改为:

MyObject func2() { return MyObject(); }

动态分配在您的情况下完全没有用。

Just change func2() into this:

MyObject func2() { return MyObject(); }

The dynamic allocation is completely useless in your case.

棒棒糖 2024-12-07 04:46:16

是的,您的第二个解决方案将解决该问题,但代价是制作更多副本。

我的问题是:为什么要在堆上分配一个对象(用 new )只是为了将其复制到堆栈分配的对象,然后立即删除它。为什么不一开始就在堆栈上创建它呢?

你可以这样写:

void main() {
    MyObject obj;
}

最后你会得到完全相同的结果。而且会简单得多!

Yes, your second solution will solve the issue, at the expense of more copies being made.

My question is: why do you want to allocate an object on the heap (with new) just to copy it to a stack allocated object, and delete it right after. Why don't you create it simply on the stack at the beginning?

You could write it as follows:

void main() {
    MyObject obj;
}

You'd get exactly the same result at the end. And it'd be much simpler!

梦一生花开无言 2024-12-07 04:46:16

恕我直言,最好的方法是删除 func1 中的一个对象(如果它必须删除 NULL)。
在 func2 中,如果我们收到空指针,我们无法以任何方式取消引用它。

IMHO, the best way for this would be to delete an object in func1, if it has to remove NULL.
In func2, in case we've null pointer received, we can't dereference it in any way.

酒解孤独 2024-12-07 04:46:16

如果您不确定 func1 是否会返回有效对象,则 boost::Optional 可能更合适,而不是返回指针 - 这会让 func2 检查 func1 是否返回了有效的内容,而不需要动态分配任何内容。

If you're not sure whether func1 will return a valid object or not, instead of returning a pointer, boost::optional may be appropriate -- this will let func2 check whether func1 has returned something valid, without making you have to allocate anything dynamically.

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