如何删除已取消引用的对象?
我有以下代码
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
一个更优雅的解决方案(并且更“正确”)是使用智能
指针:(
使用更现代的编译器,请使用 std::unique_ptr。或者如果您使用
Boost,你也可以使用
boost::scoped_ptr
。)我说得更“正确”,因为如果
MyObject
的复制构造函数抛出异常,该解决方案仍然会删除该对象,其中
因为你的会泄漏。
A more elegant solution (and more "correct") would be to use a smart
pointer:
(With a more modern compiler, use
std::unique_ptr
. Or if you're usingBoost, 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.
是的,这将解决内存泄漏问题。
一般来说,这不是一个很好的模式。但我不确定你想在这里实现什么目标!
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!
避免内存泄漏的最佳方法是使用智能指针:
这与您的解决方案几乎相同,但修复了在复制对象或执行“操作”时引发异常时您的内存泄漏问题。如果您不使用 C++11,请使用
auto_ptr
而不是unique_ptr
。The best way to avoid a memory leak is to use a smart pointer:
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 thanunique_ptr
.只需将
func2()
更改为:MyObject func2() { return MyObject(); }
动态分配在您的情况下完全没有用。
Just change
func2()
into this:MyObject func2() { return MyObject(); }
The dynamic allocation is completely useless in your case.
是的,您的第二个解决方案将解决该问题,但代价是制作更多副本。
我的问题是:为什么要在堆上分配一个对象(用 new )只是为了将其复制到堆栈分配的对象,然后立即删除它。为什么不一开始就在堆栈上创建它呢?
你可以这样写:
最后你会得到完全相同的结果。而且会简单得多!
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:
You'd get exactly the same result at the end. And it'd be much simpler!
恕我直言,最好的方法是删除 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.
如果您不确定
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 letfunc2
check whetherfunc1
has returned something valid, without making you have to allocate anything dynamically.