Thread: Memory leak prevention methodogies

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Of course you should use heap objects when you need them, but the general idea is to avoid them IF you don't need them. What you don't create, you don't need to free.
    Sorry but that is not my methodology or line of thinking about the design of my code. I know how to properly use memory and understand intimately the lifetime of my objects. Therefore whether I use heap objects or stack objects is purely a moot point. Every C++ programmer should understand how to use the heap. If you understand the heap and understand how to properly use it, I see no issues.

    I do not follow a general rule of AVOID heap objects. Everything is a tool and I just use the right tool at the right time for the right task.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Bubba View Post
    Of course you should use heap objects when you need them, but the general idea is to avoid them IF you don't need them. What you don't create, you don't need to free.
    Sorry but that is not my methodology or line of thinking about the design of my code. I know how to properly use memory and understand intimately the lifetime of my objects. Therefore whether I use heap objects or stack objects is purely a moot point. Every C++ programmer should understand how to use the heap. If you understand the heap and understand how to properly use it, I see no issues.
    Reading what you quoted, it says: "Use the heap when you need it. Don't use the heap when you don't need it." You say you disagree. Which part of that statement do you disagree with? The part about using the heap when you need it? Or the part about not using it when you don't?

    So, you seem to be making at least one, possibly both, of these two statements:

    1. You use the heap in cases where it is inappropriate.
    2. You do not use the heap in cases where it is the only way to do things.

    Which one of these statements are you making, if any?

    By using new/delete in cases where the goal could easily be accomplished another way, is to deliberately regress to a C-like worldview and all the difficulties that entails, and seems to miss the entire point of C++.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Bubba View Post
    Sorry but that is not my methodology or line of thinking about the design of my code. I know how to properly use memory and understand intimately the lifetime of my objects. Therefore whether I use heap objects or stack objects is purely a moot point. Every C++ programmer should understand how to use the heap. If you understand the heap and understand how to properly use it, I see no issues.

    I do not follow a general rule of AVOID heap objects. Everything is a tool and I just use the right tool at the right time for the right task.
    Sure, you can use heap objects whenever you want, but you can't escape from the fact that when you do, you'll also be introducing potential bugs in your code, beucase those objects must be freed, whether you like it or not.
    So in essence, if you avoid heap objects, you don't need to free. Simple as that.
    It's a good way to avoid leaks, but it's up to everyone to or not to follow the rule.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leak in this case?
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2008, 05:05 AM
  2. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 06:50 AM
  3. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  4. Any Memory Leak Checking Tool?
    By George2 in forum C Programming
    Replies: 4
    Last Post: 06-21-2006, 11:02 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM