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) );
}
Foo?delete some_pointerin the destructor, that will be a problem. Also how about your assignment operator? I thinkFoo copy = (*this);is an assignment rather than copy.Foo copy = (*this)invokes the copy constructor.