[Lemon-user] Memory leaks in code using the LEMON library and CPLEX.
Alpar Juttner
alpar at cs.elte.hu
Tue Oct 25 08:32:27 CEST 2016
Hi,
> 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.
My guess is that you probably deleted them in a wrong order. For
example, IloRange stores a reference to 'env' and may want to access it
on destruction. Thus, it will cause problems if you deconstruct 'env'
before deleting the IloRange object.
> 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.
It is a good idea in general to avoid using 'new' whenever possible.
Amongst other, you will have no problems with memory leaks, and with
the problem I described above.
> However, what solved the problem almost completely is the addition of the command
> env.end();
> before the function returns.
Well, CPLEX - contrary to LEMON - does not have a proper C++-style
memory management. You have to do it manually.
> (the remaining memory leaks were caused by the objects of a Class
> defined in our code which did not have a proper destructor defined).
Once again - use the standard stl containers (std::vectors, std::lists
etc) instead of manual memory management. Then your destructor will be
empty in most of the cases. It's very difficult to make a mistake in
implementing an empty destructor.
> 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 jut once in main.
> Then the command env.end() might not be needed at all.
Probably it's much better to do it only once.
Regards,
Alpár
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lemon.cs.elte.hu/pipermail/lemon-user/attachments/20161025/599ad172/attachment.html>
More information about the Lemon-user
mailing list