mxnet-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "McCollum, Cliff" <>
Subject Re: Reduce 99% of your memory leaks with this simple trick!
Date Thu, 11 Jan 2018 13:49:53 GMT
+1 to this.

I once worked on a 500k LOC C++ distributed system for the Telecom industry that only had
new() and delete() calls on six lines. Everything else was done via the RAII pattern with
std::  The net effect was that even in such a large system, we recorded only three memory
leaks in six years of development and production - and two of them weren’t even in our code.

Banning all use of manual memory management is absolutely possible with current C++. Anyone
calling new (unless you are using auto_ptr) should pause and look for a better way. 


Sent from my iPad

> On 11 Jan 2018, at 11:10, Pedro Larroy <> wrote:
> Hi
> I would like to encourage contributors to use RAII idioms in C++
> whenever possible to avoid resource leaks.
> RAII is an ugly acronym that stands for Resource Acquisition Is
> Initialization, which basically means that you should almost never use
> explicit new and delete operators and instead use std::make_shared,
> std::make_unique and std::vector<uint8_t>  and .data() for raw
> buffers. Also always allocating OS resources in constructors releasing
> them in destructors such as file descriptors.
> Asides from forgetting to call delete on an allocation, explicit
> deletes are bad because an exception thrown in the middle prevents
> delete from running entirely.
> This helps a lot writing correct, secure and exception safe code
> without memory leaks.
> Another problem that I think is worth a discussion, is how to handle
> exceptions and errors. Right now, I don't think there's a good way to
> throw an exception in some functions without crashing the python
> interpreter. I think we should come with a smart way to propagate
> exceptions from the library up to the user runtime (python, scala...)
> As an example of what I'm talking about is this suspicious code that I
> saw in a PR, which has several bugs in a few lines of code related to
> what I'm discussing in this thread, crashing Python when trying to
> open a file that doesn't exist. (How to propagate an exception in this
> case?)
> Please excuse the clickbait subject, just trying to grab your
> attention in a humorous way now that the weekend is approaching.
> Pedro.

View raw message