CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #16
    Join Date
    May 2004
    Posts
    474

    Re: Memory leak - arghh!

    Drama over - I malloced instead.

  2. #17
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Memory leak - arghh!

    Quote Originally Posted by richiebabes View Post
    Drama over - I malloced instead.
    That is no solution.

    First, if that class is non-POD, you cannot create objects using malloc(). All malloc() does is allocate bytes, that's it. The only way to actually create an object is to either declare one, or create one dynamically using operator new.

    Using malloc() is equivalent to having me dump all the parts of a car in front of you. You can't drive it, you can't open the door, but all the parts are on the floor sitting there. On the other hand, operator new (or declaring an object) is equivalent to having the car built, ready to drive, open the door etc.

    Regards,

    Paul McKenzie

  3. #18
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Memory leak - arghh!

    Quote Originally Posted by richiebabes
    Drama over - I malloced instead.
    Granted, since you are creating an array of char, malloc() will work, but it is walking a tight rope to use malloc() in C++ since you could accidentally use it for a non-POD type.

    Frankly, a far simpler and safer solution would be:
    Code:
    std::vector<char> chpts(sPoints.GetLength() + 1);
    Now, you no longer need to worry about managing the memory for chpts, and if you do need a char*, just write &chpts[0].
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #19
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Memory leak - arghh!

    Quote Originally Posted by richiebabes View Post
    Drama over - I malloced instead.
    God sakes man you would still have to free!
    Stop trying to take some shortcut!
    Do it for yourself.

    What you really need to do is to get each pointer from your shapes and call delete on them after you are done with them
    but before you remove them from your CArray.
    Last edited by ahoodin; March 16th, 2009 at 09:42 AM.
    ahoodin
    To keep the plot moving, that's why.

  5. #20
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Memory leak - arghh!

    Quote Originally Posted by laserlight View Post
    Granted, since you are creating an array of char, malloc() will work, but it is walking a tight rope to use malloc() in C++ since you could accidentally use it for a non-POD type.
    I don't think it's an array of char that's being created, but an array of CShape. That sounds like it's non-POD, making the malloc() "solution" incorrect.

    Regards,

    Paul McKenzie

  6. #21
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Memory leak - arghh!

    Ok you have all sorts of good advice. Now try to take it. Short of VNCing your desktop there is alot of good stuff here. Now empower yourself, and do it!
    ahoodin
    To keep the plot moving, that's why.

Page 2 of 2 FirstFirst 12

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