Re: Help!!! Out of Memory!!!
Typically an "Out of Memory" error message means just that; however, what type of memory (yes there are different types) is another story. Almost every time I have received this message, it was because I was allocating memory in a loop and never released it. I am afraid you are going to have to go on a bug hunt to see if you are leaking memory. MSDN and the discussions in this forum may be of assistance. Do a search on "Memory Leak" for some hints on how to proceed.
Re: Help!!! Out of Memory!!!
Hello,
Could you give me a hand on how to free up memory? I think I have to go back to the basics. The following is a segment of my program. Do you see any memory leak?
int Transport::sendReq(const char *req) { int reqlen = strlen(req);
char *bufTemp = new char[reqlen+2];
strcpy(bufTemp, req);
bufTemp[reqlen] = FRAMECHAR;
pSocket->Send(bufTemp, reqlen+1, 0);
return 0;
}
Thank you for the reply. : )
/*^_____________^*/
Re: Help!!! Out of Memory!!!
you should put "delete bufTemp;" before the return
Re: Help!!! Out of Memory!!!
you should put "delete [] bufTemp;" before the return
Re: Help!!! Out of Memory!!!
Hi,
Thanks for the reply.
Why isni't bufTemp freed up when the function ends? Could you give me a general rule when to "delete" the variables?
Thank you.
/*^_____________^*/
Re: Help!!! Out of Memory!!!
Here is the rule: When ever you use New you must always call delete even if the function is going out of scope because new creates the data type on the heap. If it goes out of scope before you delete it, you my friend have just created a memory leak. Hope this is helpful
Re: Help!!! Out of Memory!!!
ThanQ__^
/*^_____________^*/
Re: Help!!! Out of Memory!!!
Hi
when do you use
delete buffer;
delete [] buffer;
thanks
chrislaw
Re: Help!!! Out of Memory!!!
You use delete [] when you allocate an array. In your example, you say new char[number+2]
By using char[number+2]
, that is an array. So therefore you must use delete []
If you do new char
, then you only have to use delete
Re: Help!!! Out of Memory!!!
Basic rule of thumb that I go by is if you have a new you have to have a delete somewhere to go with it. If it is a new in your constructor you should have a delete in your destructor. If you have a new in a function you should have a delete before leaving the function.
Mike
Re: Help!!! Out of Memory!!!
Actually bufTemp is being freed up when it goes out of scope; however, bufTemp is only a pointer.
When it goes out of scope the pointer is freed not the memory it pointed to. So what you wind up with is a char array in memory that can't be referenced (the pointer is gone so you really don't know where it is).
Since it is an array you must use delete[] to free it before it goes out of scope.
If it wasn't an array you would just use delete
There are problems similar to the new/delete pair when you use GDI resource. Before you use them read up on them in MSDN. I had a real nasty leak once because of a bitmap resource that I allocated and didn't free.
Re: Help!!! Out of Memory!!!
Hi all,
Thank you for replying.
Now I have some sort of idea of pairing up new and delete. However, in the code below (I wrote it...)
void EMSClient::showMeetings(const char *name)
{
int length = strlen(packingsPtr->pack("00", "03", (char*)name));
char * message = new char[length];
strcpy (message, packingsPtr->pack("00","03", (char*)name));
MCSp->send(EMScommon::EMSCHAN, message);
delete [] message;
}
The last line, "delete [] message", raises a DEBUG FAILED message. If I remove the line, the program does not crash. What is the problem here? Am I missing anything important?
Thank you. : )
/*^_____________^*/
Re: Help!!! Out of Memory!!!
Simple; you didn't make room for the terminating NULL in the string:
int length = strlen(packingsPtr->pack("00", "03", (char*)name));
char * message = new char[length + 1]; // <<--Note!!
strcpy (message, packingsPtr->pack("00","03", (char*)name));
Regards,
Paul McKenzie
Re: Help!!! Out of Memory!!!
Hello,
Yes, when I add the +1 and delete, it works. Could you explain why this is causing the problem? Thank you. : )
/*^_____________^*/