Auto Pointer Issue
I'm new to C++ and a bit confused regarding auto_ptr.
I have a class which inside has a static auto_ptr.
static std::auto_ptr<MyCompany::CConnection> con = std::auto_ptr<MyCompany::CConnection> (util::getDBConnection() );
Util::getDBConnection() implementation :
CConnection* util::getDBConnection(){
try
{
cout<< &MyCompany::GetFermatConnection();
return &MyCompany::GetFermatConnection();
}
catch(...)
{
//connect to local DB
throw;
}
}
However when my program finished, it always hit an exception in memory, during the destructor of auto pointer.
~auto_ptr()
{ // destroy the object
if (_Myptr != 0)
delete _Myptr; // exception in this line.
}
The exception is "Unhandled exception at 0x00000001800024e8 in TestDLL.exe: 0xC0000005: Access violation reading location 0xffffffffffffffff."
I understand that auto_ptr will try release any memory when it reach the end of its scope. But, in this case I don't have any idea what goes wrong. Does anyone know what is the possible cause?
Although you've shown the implementation of util::getDBConnection
, it doesn't really answer the question, which is whether what it returns is ultimately a pointer that was allocated with new
.
If it was, then an error message when you attempt to delete that pointer indicates that your heap has probably gotten corrupted (quite possibly in some completely unrelated code).
If it's returning something that wasn't allocated with new
, then the problem is even simpler -- since auto_ptr
uses delete
on the pointer, it can only be used with something that was allocated with new
. Using it on a pointer that was allocated any other way will give UB.
I don't know the specifics of your code, but I would guess that there is code that sets pointers to -1 when deleting them, and that the crash is from a double-delete on the same pointer.
COM and auto_ptr probably shouldn't be mixed, which is what it appears is happening given the COM:: portion of the code.
auto_ptr is more of a 'strong pointer' - It owns it and deletes it unless it's ripped away.
COM uses a ref counted model. Objects are created with reference count 1. Any new reference should be AddRef'd and when no longer needed, Release().
Look at a COM specific smart pointer like this instead.
Apologies if I am incorrectly guessing about the COM scenario.
链接地址: http://www.djcxy.com/p/79940.html上一篇: 在C ++ pimpl中使用void指针的优点和缺点
下一篇: 自动指针问题