CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Oct 1999
    Location
    Canada
    Posts
    62

    Help!!! Out of Memory!!!

    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. : )

    /*^_____________^*/

  2. #2
    Join Date
    Apr 1999
    Location
    Michigan, USA
    Posts
    115

    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.


  3. #3
    Join Date
    Oct 1999
    Location
    Canada
    Posts
    62

    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. : )

    /*^_____________^*/

  4. #4
    Join Date
    Oct 1999
    Location
    israel
    Posts
    48

    Re: Help!!! Out of Memory!!!

    you should put "delete bufTemp;" before the return


  5. #5
    Join Date
    Apr 1999
    Posts
    396

    Re: Help!!! Out of Memory!!!

    you should put "delete [] bufTemp;" before the return


  6. #6
    Join Date
    Oct 1999
    Location
    Canada
    Posts
    62

    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.

    /*^_____________^*/

  7. #7
    Join Date
    Nov 1999
    Posts
    51

    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


  8. #8
    Join Date
    Oct 1999
    Location
    Canada
    Posts
    62

    Re: Help!!! Out of Memory!!!

    ThanQ__^

    /*^_____________^*/

  9. #9
    Join Date
    Oct 1999
    Location
    switzerland
    Posts
    23

    Re: Help!!! Out of Memory!!!

    Hi

    when do you use

    delete buffer;
    delete [] buffer;



    thanks

    chrislaw

  10. #10
    Guest

    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




  11. #11
    Guest

    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


  12. #12
    Join Date
    Apr 1999
    Location
    Michigan, USA
    Posts
    115

    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.


  13. #13
    Join Date
    Oct 1999
    Location
    Canada
    Posts
    62

    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. : )

    /*^_____________^*/

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    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


  15. #15
    Join Date
    Oct 1999
    Location
    Canada
    Posts
    62

    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. : )

    /*^_____________^*/

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured