Thread: Bad Ptr?

  1. #1
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215

    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?
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I think that whatever you're trying to delete wasn't allocated.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    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?
    Last edited by durban; 10-29-2005 at 04:37 PM.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  4. #4
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    Please realize that it is not NULL unless you assign it to that. So if you just do:

    Code:
    char *myPtr;
    then myPtr isn't NULL by default. Also, whne you delete it, you need to use "delete[] Data", not just "delete Data".

  5. #5
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    ? Look at my code rocky
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    As rockytriton said a variable isn't NULL unless you initialize it to NULL. Try setting m_Data to NULL in your constructor.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling c++ intrinsics commands
    By h3ro in forum C++ Programming
    Replies: 37
    Last Post: 07-13-2008, 05:04 AM
  2. Poker bad beats
    By PJYelton in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 01-15-2005, 11:42 PM
  3. Pointers in C
    By Nancy7904 in forum C Programming
    Replies: 21
    Last Post: 12-16-2004, 10:51 PM
  4. Shocking(kind of)
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 12-10-2002, 08:52 PM
  5. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 07:31 AM