Lu Yinghai
Lu Yinghai

Reputation: 21

Is there any way to prevent memory leak without asking developers to pay extra attention to it in C++

Currently, we have several way to prevent memory leak such as

  1. proxy(shared_ptr, auto_ptr) and
  2. book keeping method,
  3. garbage collection(java)

But the former one needs much overhead from developers and the later one cause much resource overhead.

Is there any other way that is resource efficient and free developers from this problem?

Upvotes: 2

Views: 168

Answers (3)

Dennis
Dennis

Reputation: 3731

Well good coding practising become good programming habits and then there is very little "attention" required in order to utilise them. I would suggest using RAII wrapper objects such as std::unique_ptr, std::shared_ptr, alongside clear guidelines on when and where your should use dynamic allocation.
A good place to start is that if you see anyone typing the word delete then it should be a flag that you need to be extra careful.

This brings me to my second point: Don't use dynamic allocation (unless absolutely necessary), use automatically destroyed scope-controlled objects as much as possible. NEVER new an RAII object, and this will force you to think about how the object that the RAII owner wraps should be handled if the scope changes.

For example:

std::auto_ptr<Foo> ptr2Foo( new Foo() );
...
...
if(ptr2Foo->isValidNow())
    passToOwningObject(otherObject, ptr2Foo);
//now when scope ends the newed Foo will be destroyed or owned
//by an appropriate object.

or

std::unique_ptr<Foo> ptr2Foo( new Foo() );
...
...
if(ptr2Foo->isValidNow())
    passToOwningObject(otherObject, std::move(ptr2Foo));
//now when scope ends the newed Foo will be destroyed or owned
//by an appropriate object.

And run code anaylsis (MSVC, Valgrind, Coverity etc...) regularly, and don't ignore compiler warnings.

Upvotes: 0

Grimm The Opiner
Grimm The Opiner

Reputation: 1806

I've always felt using references, return and pass by reference, and const correctness as exactly as possible can take care of much of it. You can't delete a reference, so you're not expected to. You can't delete a const pointer, so you're not expected to. Then on those RARE occasions a coder is passed a non const pointer they'll take notice.

Pay real attention to the idea of ownership of heap allocated objects and reveal/expose them grudgingly if at all.

Also, write more complete interfaces to give clues, for example:

class DataWrapper
{
public:
    const BYTE* PeekAtData() const;        
    BYTE* RemoveData();
....
};

Upvotes: 0

Alok Save
Alok Save

Reputation: 206508

Is there any way to prevent memory leak without asking developers to pay extra attention to it in C++?

Use minimal dynamic allocations, infact only when strictly necessary.
If you are using Dynamic allocations you will have to obey the price it comes with and that is handling it correctly. The best way to do so is using RAII in C++, Note that writing an RAII code is not trivial but with practice it one gets used to thinking in RAII way.

Upvotes: 7

Related Questions