Hacker News new | past | comments | ask | show | jobs | submit login

What is your rationale for tip #2? Don't forget that you also have to guard against other kinds of leaks: handles, database connections, etc. Given the lack of a finally clause in standard C++ try/catch blocks I'm not sure how you can do that successfully w/o smart pointers and their ilk.



Of course C++ has RAII, as you must know. Allocate the object on the stack or, if you must, use a stack-allocated auto_ptr if you want it to last as long as the block, as the destructors for stack-allocated objects form the hidden "finally" clause of every block. Otherwise, it is stored somewhere, and the object that stores it should manage it. If you store an object in many different places with no master location, you are starting to wander into automatic memory management territory.

As for my rationale, reference counting is by most accounts an obsolete form of garbage collection. The smaller your object is, the more space you waste. If your references are at all cyclic, you have to introduce weak references, in which case you must understand your memory structure to the point where you should be able to manage your memory without garbage collection. (Languages such as Java contain weak references, but are never needed and only should be used for things that the programmer is willing to lose such as a cache.) Cyclic references will creep up on you when you least expect it.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: