-
Bad Ptr?
Hey all, I'm getting an unhandled exception when I try to delete something off of the heap and when I hover over it, it says: 0x2850009c <Bad Ptr>. Anyone know what could cause this? As far as I can see, everything looks right but the code is large so I might be missing something. Anyone had any experience with this type of error?
-
I think that whatever you're trying to delete wasn't allocated.
-
It was allocated, it would be at address 0x00000000 if it wasn't. Heres some code though:
Code:
void ZeroPacket::Free()
{
if( m_Data ) delete [] m_Data;
}
That is the line I get the error on, m_Data is the Bad Ptr supposedly.
Code:
BOOL ZeroPacket::SetData(const pcap_pkthdr *header, const u_char* data)
{
if( !header || !data )
return FALSE;
m_header = *header;
m_Data = new u_char[m_header.len+1]; //// m_Data is allocated
memcpy(m_Data, data, m_header.len);
return TRUE;
}
The above is self-explanatory, it allocates m_Data and copy's a packet header to it.
Code:
BOOL ZeroPacketData::SetData(ZeroPacket* Data)
{
if( Data == NULL )
{
//! Do Error
return FALSE;
}
if( !Data->IsLoaded() )
{
//! Do Error
return FALSE;
}
m_Packets.push_back(*Data);
delete Data; // CALLS THE DECONSTRUCTOR, THUS CAUSING THE ERROR
return TRUE;
}
delete Data calls the deconstructor for ZeroPacket and then calls Free().
EDIT: While it is being allocated, somewhere along the line its either losing the data written at that location, or its being turned to garbage data. So would it be ok not to delete it? Would that lead to a memory leak?
-
Please realize that it is not NULL unless you assign it to that. So if you just do:
then myPtr isn't NULL by default. Also, whne you delete it, you need to use "delete[] Data", not just "delete Data".
-
-
As rockytriton said a variable isn't NULL unless you initialize it to NULL. Try setting m_Data to NULL in your constructor.
-
Quote:
Code:
void ZeroPacket::Free()
{
if( m_Data ) delete [] m_Data;
}
Such protection is not needed. The c++ standard says that deleting a NULL pointer is ok.