Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a array of CEdit*, if this array has elements that aren't NULL I have to delete these..I wrote:

C++
if (m_arEdit.GetSize() > 0)
	{
		for (int j = 0; j < m_arEdit.GetSize(); j++)
			if(m_arEdit[j] != NULL)
			delete m_arEdit[j];  //in this point software crashes because the element is NULL but it results not NULL

	}



but when in debug m_arEdit is NULL the row if(m_arEdit[j] != NULL)
is true and software crashes..

What I have tried:

I tried to write the code how I told but it isn't correct and I don't know why
Posted
Updated 20-Mar-24 2:07am
v3
Comments
Richard MacCutchan 20-Mar-24 4:39am    
comment deleted.
Michael Hulthin 20-Mar-24 9:24am    
Looks like you're trying to delete bogus pointers, either because not properly initialized or you are accidently deleting them more than once?

How is m_arEdit initialized?
Will the entries always either be a nullptr or a valid CEdit* ?

To be safe I would also set the element to NULL after the delete and also you don't have to check for NULL, delete can handle null values.

Have you tried calling m_arEdit[j]->AssertValid(); for debug info (I think MFC has a helper method called that).

It's been a long time since I used MFC ... but your problem is simpler than you think: delete is used to return memory to the heap, it won't remove text from a CEdit control.

And that's why your app crashes: the chunk of memory you are trying to delete isn't allocated separately with new so the system gets confused and everything breaks down.

Instead of delete use the CEdit methods to remove the text line: CEdit Class | Microsoft Learn[^]

And just as an aside, never delete items from the beginning - always delete from the end and work back.
Why? Two reasons: firstly it eliminates a heck of a lot of unnecessary copying, and secondly, it means you look at every item. If you delete from the start and two items next to each other need to be removed, then removing the first one and incrementing the index means you don't look at the second.

I'd also think pretty hard about what exactly you are doing: why are you removing all the non-empty items? Wouldn't it be more sensible to remove everything in one go since you will end up with an "empty" control either way?
 
Share this answer
 
Comments
Member 14594285 20-Mar-24 4:49am    
Ok..I would like to deallocate memory so I must use delete..
I am assuming that you are calling this code more than once, and the second time you are trying to delete an entry that has already been deleted. You should change the code to:
C++
for (int j = 0; j < m_arEdit.GetSize(); j++)
{
    if(m_arEdit[j] != NULL)
    {
        delete m_arEdit[j];
        m_arEdit[j] = NULL; // ensure this entry is not tried again
    }
}
 
Share this answer
 
Comments
Member 14594285 20-Mar-24 4:53am    
it isn't correct because software crashes, I have the same problem how I told
Richard MacCutchan 20-Mar-24 5:01am    
Well your question is not clear, so please explain exactly where it crashes, and what variable values are the cause.
Member 14594285 20-Mar-24 5:23am    
I improved question
Richard MacCutchan 20-Mar-24 5:30am    
Your explanation does not make sense. If the item is NULL then the test will skip the delete. You need to do some more debugging to see exactly what is happening.
Member 14594285 20-Mar-24 7:12am    
in debug I see that hwnd is NULL, the address isn't null and the delete isn't skip
Building on Richard's answer, I would make a minor change.
C++
int size = m_arEdit.GetSize();
for (int j = size - 1; j >= 0; j--)
{
    if(NULL != m_arEdit[j])
    {
        delete m_arEdit[j];
        m_arEdit[j] = NULL;
    }
}
 
Share this answer
 
Comments
Richard MacCutchan 20-Mar-24 7:10am    
Forwards or backwards should not matter, as he is not removing the array items. Or do I misunderstand something about CArray?
Pete O'Hanlon 20-Mar-24 7:14am    
I'm not sure what optimisations have been put in place in CArray since I last used it. This approach was one I have used since the mid 90s, going backwards when removing items to avoid *clever* compiler tricks.
Richard MacCutchan 20-Mar-24 7:30am    
Thanks, I can't actually remember using CArray, but it is 20 years or so since I last used MFC.
I think the Richard's solution is probably the correct, but other possibilities:

1. How do you construct and initialize the m_adEdit array?. If you created it with new and not initialize it, maybe they contain some chunk. Initialize it with memset(m_arEdit,0,sizeof(CEdit*)*n).
2. The CEdit objects you pointed are created with new (in the heap) or, maybe you pointed a CEdit in stack with the & operator? In this case the delete will crash.

It would help if you show how you initialize m_arEdit and how you assign the elements to point CEdit.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900