返回带有重载运算符的副本
我有一个类 Foo
,我已为其重载了 + 运算符,如下所示:
Foo Foo::operator+(const Bar &b)
{
Foo copy = (*this);
if (someCondition) return copy;
//snip
}
对我来说,这看起来很合理。但是,每当我返回副本时,Visual Studio 都会提醒我一个错误,“可能是由于堆损坏”。我所做的事情有什么问题吗?
编辑:更新更多信息。
错误消息:
Windows 已触发断点 示例.exe。
这可能是由于 堆,这表明存在错误 Sample.exe 或其具有的任何 DLL 已加载。
这也可能是由于用户 当sample.exe有时按F12 重点。
输出窗口可能有更多 诊断信息。
复制构造函数:
Foo::Foo(const Foo&p)
{
some_pointer = p.get_some_pointer();
some_value = p.get_some_value();
}
它分解为的代码:
//within dbgheap.c
extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
const void * pUserData
)
{
if (!pUserData)
return FALSE;
if (!_CrtIsValidPointer(pHdr(pUserData), sizeof(_CrtMemBlockHeader), FALSE))
return FALSE;
return HeapValidate( _crtheap, 0, pHdr(pUserData) );
}
I have a class Foo
for which I've overloaded the + operator as follows:
Foo Foo::operator+(const Bar &b)
{
Foo copy = (*this);
if (someCondition) return copy;
//snip
}
To me, this looks reasonable. However, whenever I am returning copy, Visual Studio alerts me of an error which 'may be due to a corruption of the heap'. Is there something wrong with what I've done?
edit: updating with more info.
The error message:
Windows has triggered a breakpoint in
sample.exe.This may be due to a corruption of the
heap, which indicates a bug in
sample.exe or any of the DLLs it has
loaded.This may also be due to the user
pressing F12 while sample.exe has
focus.The output window may have more
diagnostic information.
The copy constructor:
Foo::Foo(const Foo&p)
{
some_pointer = p.get_some_pointer();
some_value = p.get_some_value();
}
The code it breaks to:
//within dbgheap.c
extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
const void * pUserData
)
{
if (!pUserData)
return FALSE;
if (!_CrtIsValidPointer(pHdr(pUserData), sizeof(_CrtMemBlockHeader), FALSE))
return FALSE;
return HeapValidate( _crtheap, 0, pHdr(pUserData) );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种类型的错误通常与同一指针的多次删除(或释放)相关,或者与一些更模糊的情况相关(从一个堆获取并释放到另一堆,但这里可能不是这种情况)。
我要做的第一件事是查看析构函数并检查您是否没有进行浅复制和双重删除。例如,使用以下代码:
如果是这种情况,您可以决定是否要进行浅复制或深复制。在第二种情况下,复制构造函数(和赋值运算符)应该分配自己的内存,而在第二种情况下,您必须确保内存不会被释放两次。
与指针一样,最好将资源管理委托给外部(预构建)类。在唯一所有权(和深拷贝)的情况下,您可能应该使用
std::auto_ptr
(或 C++0x 中的std::unique_ptr
——或者 boost 变体)。在第二种情况下,使用boost::shared_ptr
(或 C++0x 中的std::shared_ptr
)将确保数据共享且仅删除一次。That type of error is usually associated with multiple deletions (or frees) of the same pointer, or with some more obscure situations (acquiring from one heap and releasing to a different heap, but that is probably not the case here).
The first thing I would do is look a the destructors and check that you are not shallow copying and doubly deleting. For example with the following code:
If this is the case, you can decide whether you want to have shallow copies or make deep copies. In the second case the copy constructor (and assignment operator) should allocate their own memory, while in the second case you must ensure that the memory is not released twice.
As always with pointers, it is better to delegate resource management to external (pre-built) classes. In the case of unique ownership (and deep copies) you should probably use
std::auto_ptr
(orstd::unique_ptr
in C++0x -- or the boost variants). In the second case, usingboost::shared_ptr
(orstd::shared_ptr
in C++0x) will ensure that the data is shared and only deleted once.