[Lemon-user] Memory leaks in code using the LEMON library and CPLEX.

Spyros Vassilaras svasilaras at gmail.com
Tue Oct 25 00:46:07 CEST 2016


 Dear Alpár,

 Thank you for your prompt reply.
 Indeed you suggestion is correct and further reduced the memory leak
problem. For some reason I was not able to delete the objects created by
"new" at the end of the function, as this caused the program to crash. So,
I replaced this line with:
Graph::ArcMap<IloRange> capacityConstraints(network, IloRange(env, 0 , 0));
and did the same for other similar lines found in the code. (In fact, I did
not wrote this code. I inherited it from a colleague and I am building on
it). The code seems to work as before and the memory leak is alleviated.
However, what solved the problem almost completely is the addition of the
command
env.end();
before the function returns. (the remaining memory leaks were caused by the
objects of a Class defined in our code which did not have a proper
destructor defined).
I am wondering if it is a good practice to create and end the CPLEX env
within a function, or it is better to create it just once in main. Then the
command env.end() might not be needed at all.

 Best regards,
 Spyros

On Mon, Oct 24, 2016 at 11:15 AM, Alpar Juttner <alpar at cs.elte.hu> wrote:

> Hi,
>
> Graph::ArcMap<IloRange> capacityConstraints(network, * new IloRange(env, 0
> , 0));
>
>
> This line certainly looks quite badly. You allocate a temporary object
> 'IloRange' with 'new', but you do not 'delete' it.
>
> Then and initialize the ArcMap by copying the temporary object for each
> existing Arcs, this may or may not be what you want.
>
> According to this post: http://lemon.cs.elte.hu/pipermail/lemon-user/2013-
> March/000682.html , I was expecting that all the variables created within
> the function should be cleared up when exiting the function. But this is
> not the case.
>
>
> You rightly expected - all the local variables are cleaned up on exit. But
> the ones you allocated by 'new' are not local variables.
> This is not LEMON specific, but how C++ works in general.
>
> Regards,
> Alpár
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lemon.cs.elte.hu/pipermail/lemon-user/attachments/20161025/2b542eab/attachment.html>


More information about the Lemon-user mailing list