Error while using delete

I allocated ane char pointer dynamically and then i m trying to delete it using delete operator... But strangely its giving error....


i tried following code..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
bool ArcaFastRetransmissionServerGD :: RequestRetransmission
(	
	unsigned_long_long			PStartSequence, 
	unsigned_long_long			PEndSequence 
)
{
....
char *retransmissionMsg = new char[sizeof(ArcaL2RetransRequest_t)];

....
RequestQueue.append(retransmissionMsg);
.....
}


void ArcaFastRetransmissionServerGD :: ProcessNextRequest()
{
.......
char * RTInfo =RequestQueue.removeFirst();

 while(!RequestQueue.isEmpty())
 {
   .......
            delete RTInfo;
 }
}



Its giving memory dump when we execute delelte stmt
Last edited on
Note that if you dynamically create a char array with char *retransmissionMsg = new char[size] you have to delete it with delete it with delete [] retransmissionMsg not delete retransmissionMsg.

Btw i can't see where you're deleting the array in the code you posted above
I m putting my retransmissionMsg in a Queue and then i m removed ot from Queue in some another fumction...
delete it with delete it with delete [] retransmissionMsg not delete retransmissionMsg.

I tried using delete[] but giving error...
The snippet you post allocates memory for a variable named retransmissionMsg and deallocates memory through a variable name RTInfo. Since you're allocating something and deallocating something else, there's no way to tell what's wrong there.
Hi fillipe
I updated code,
wat i m trying to do is I m allocating memory for char pointer pointing to chat array(retransmissionMsg), I am putting that pointer in a Queue(u may call it as global queue...). And from another function i m removing first element from queue and after performing some operation i m trying to delete it...
1
2
3
4
5
6
 while(!RequestQueue.isEmpty())
 {
            char * RTInfo =RequestQueue.removeFirst();
   .......
            delete[] RTInfo;
 }

I think that you're deleting RTInfo twice.
Check removeFirst() to actually pop the pointer, and return its value.
So u want me to remove delete[] RTInfo;
statement???
I didnt get u???
void ArcaFastRetransmissionServerGD :: ProcessNextRequest() is supposed to clear the queue, or just get rid of the one on top?

I think of this sources of error:
_ RequestQueue.removeFirst() not popping or not returning the correct value.
_ Changing RTInfo inside the while (losing the address) (that's why I put the assign inside the while)

Comment the delete [] and check if your program works exactly as is supposed to do.


You can forget about delete and new using string or vector<>

Hey guys program is working now....
What i have done is i made RTInfo point to NULL and then applied delete..
i.e.

1
2
RTInfo=NULL;
delete RTInfo;


Is it fine ???(As per performance wise as prog is working now)....
Well, if RTInfo was the only pointer pointing to something created with new, that's a memory leak. That delete RTInfo; line isn't doing anything.
Last edited on
Well, if RTInfo was the only pointer pointing to something created with new, that's a memory leak. That delete RTInfo; line isn't doing anything.
so wat shld probably i do?????
What's in RequestQueue.removeFirst()?
What's in RequestQueue.removeFirst()?


RequestQueue is a queue---
and RequestQueue.removeFirst() will rmove first element from that queue.....
Topic archived. No new replies allowed.