Thread: deleting values of a CArray?

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

    deleting values of a CArray?

    Hey all, I'm having a problem with freeing memory. I have a CArray composed of pointers to classes allocated on the heap(via new). The problem is on destruction. The code is simple and syntax correct, I'm just getting an Unhandled Exception when it tries to free it(It's not NULL, 0xccd0323 is an example of the address). This could be corrupt data, but I am sure it isn't corrupt before it is being freed because I can use the data throughout the program. Here's how I delete them:

    Code:
    BOOL ZeroPacketData::ClearAllData()
    {
    	for( int i=0; i<m_Packets.GetCount(); i++ )
    		delete m_Packets.GetAt(i);
    
    	m_Packets.RemoveAll();
    	return TRUE;
    }
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    how are you adding the items to the CArray and how are you allocating them?

  3. #3
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    This is how I add data to the CArray:

    Code:
    BOOL ZeroPacketData::SetData(ZeroPacket* Data)
    {
    	if( Data == NULL )
    	{
    		//! Do Error
    		return FALSE;
    	}
    	if( !Data->IsLoaded() )
    	{
    		//! Do Error
    		return FALSE;
    	}
    
    	m_Packets.Add(Data);
    	return TRUE;
    }
    EDIT: forgot the allocation:

    Code:
    LRESULT CStageZeroNetworkAnalysisView::OnNewPacket(WPARAM wParam, LPARAM lParam)
    {
    	ZeroPacket* ZP = new ZeroPacket();
    	ZP->SetData((pcap_pkthdr*)wParam, (u_char*)lParam);
    	ZP->Serialize(ZData);
    Serialize just calls SetData() which is above.
    Last edited by durban; 10-31-2005 at 04:33 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
    everything seems to be ok, the only two things I can think of are:

    1. You are deleting the pointers somewhere else. Once you call delete, the pointer address stays the same unless you manually set it to NULL, so trying to delete again will cause an error.

    2. There is a memory problem somewhere else in the program that causes the offsets to change, these kind of errors are very difficult to find in a large program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  2. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  3. Return Values
    By Drahcir in forum C Programming
    Replies: 4
    Last Post: 03-21-2006, 12:15 AM
  4. Sending values to a control
    By Zyk0tiK in forum C Programming
    Replies: 6
    Last Post: 12-02-2005, 06:29 PM
  5. How to compare CString and CArray?
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2002, 11:51 AM