mxnet-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pedro Larroy <pedro.larroy.li...@gmail.com>
Subject Reduce 99% of your memory leaks with this simple trick!
Date Thu, 11 Jan 2018 11:09:59 GMT
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?)

https://github.com/apache/incubator-mxnet/pull/9370/files

Please excuse the clickbait subject, just trying to grab your
attention in a humorous way now that the weekend is approaching.

Pedro.

Mime
View raw message