Click to See Complete Forum and Search --> : Help!!! Out of Memory!!!
smile
October 31st, 1999, 10:38 PM
Hi All,
I wrote a communication program using CAsyncSocket. When I run it for some time (a very short time, and at the same point), I always get a warning message, "Out of Memory". Does anyone know what this means and how to resolve it?
Any help is appreciated. Thank you. : )
/*^_____________^*/
Gary Grant
November 1st, 1999, 09:56 AM
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.
smile
November 1st, 1999, 07:15 PM
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. : )
/*^_____________^*/
miki watts
November 1st, 1999, 09:45 PM
you should put "delete bufTemp;" before the return
Todd Jeffreys
November 1st, 1999, 11:14 PM
you should put "delete [] bufTemp;" before the return
smile
November 2nd, 1999, 12:04 AM
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.
/*^_____________^*/
Craig Beasley
November 2nd, 1999, 12:44 AM
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
smile
November 2nd, 1999, 12:53 AM
ThanQ__^
/*^_____________^*/
claw
November 2nd, 1999, 02:39 AM
Hi
when do you use
delete buffer;
delete [] buffer;
thanks
chrislaw
November 2nd, 1999, 08:00 AM
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
November 2nd, 1999, 08:12 AM
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
Gary Grant
November 2nd, 1999, 10:41 AM
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.
smile
November 2nd, 1999, 10:55 AM
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. : )
/*^_____________^*/
Paul McKenzie
November 2nd, 1999, 11:30 AM
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
smile
November 2nd, 1999, 11:36 AM
Hello,
Yes, when I add the +1 and delete, it works. Could you explain why this is causing the problem? Thank you. : )
/*^_____________^*/
Paul McKenzie
November 2nd, 1999, 11:59 AM
A NULL terminated string (also called an ASCIIZ string) must have room for the characters *and* the '\0' value. Unlike Pascal or some other languages, in C or C++, the '\0' character determines the end of an array of characters. So if you have a string that is 10 characters, actually it is 11 characters because of the terminating '\0' value.
What you did is that you overwrote memory. You only allocated "length" characters, but you called strcpy(), which copies the source string (which is "length" characters in your example), *plus* the terminating NULL. The overwrite occurred because of the NULL being written in a location that wasn't allocated.
The bottom line is that just being off by one byte can cause a program to crash.
Regards,
Paul McKenzie
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.