delete multiple pointers = crash

This is what is going wrong-

1
2
3
4
5
6
7
8
9
10
11
12
pick* pointer1 = NULL 
pointer1 = (pick*)m_pMain->m_port.m_SockArray[casted_port[j]];

pick* pointer2 = NULL 
pointer2 = (pick*)m_pMain->m_port.m_SockArray[casted_port[j]];

if(!pointer1)
{
     delete pointer1;
     delete pointer2;  //crashes
     return;
}


it crashes the app when deleting the 2nd pointer, what is the correct way in dealing with this? both are set in the same function.

code updated
Last edited on
Class is a keyword, so you can't name a pointer class. You're also trying to assign pointers to the address of a constant, which makes no sense. You're not using the dereference operator, and you can't assign a value to a null pointer like that anyways. It needs to have the address of a variable first. Lastly, you're trying to delete the pointers when you never allocated memory dynamically for them. The delete operator should only be used with the new operator.
You are trying to delete an object that was never created. Your pointer2 is pointing to memory value 20, and then you're trying to delete an object of type class at location 20 (assuming your compiler lets you give a pointer a value like that without having to cast it specifically), but you never created an object at that location, so it's all going wrong.

It's just as wrong with pointer1.

You can only delete something that you created with new. You are not deleteing a pointer; you are deleting some object that the pointer points at.
Last edited on
Use delete to unallocate the storage that was allocated using new. The code above dosn't use new so I would assume that undefined behavour is to be expected when delete is called.

http://www.cplusplus.com/reference/std/new/operator%20delete/
my app is using alot of pointers, which is the best way to free up the memory when they are no longer needed?

this is part of the code.

1
2
3
4
5
6
7
8
9
10
11
12
pick* pointer1 = NULL 
pointer1 = (pick*)m_pMain->m_port.m_SockArray[casted_port[j]];

pick* pointer2 = NULL 
pointer2 = (pick*)m_pMain->m_port.m_SockArray[casted_port[j]];

if(!pointer1)
{
     delete pointer1;
     delete pointer2;  //crashes
     return;
}


which is the best method here to free up the memory? using delete or some other function?
Last edited on
If you didn't create an object using new, then it's not your problem and there's nothing you can do about it anyway. The memory will be reclaimed when the object goes out of scope.

You are not creating pointers using new, so you cannot free the memory they are taking. It will be done when the pointers go out of scope.

You do not appear to be creating the objects the pointers are pointing at using new, so you cannot free the memory those objects are taking. It will be done when the objects go out of scope.

my app is using alot of pointers, which is the best way to free up the memory when they are no longer needed?

A pointer often takes up the same amount of memory as an int; on the system I'm typing this on, four bytes. If you are so short of memory that four bytes is an issue, I think you need to rethink your design.

I think you have misunderstood the relationship between new, delete and pointers; possibly, you've misunderstood what a pointer is as well.

Last edited on
Topic archived. No new replies allowed.