Originally posted by: DavidNLarsson
Hi,
I'm currently working on a problem where I'm implementing a Branch and Price algorithm, using ILOG CPLEX as the LP solver.
Implementation is made in C++, using the ILOG Concert C++ API.
Now, as I'm visiting nodes in my BNP tree, I naturally call some of my own functions (column generation etc) repeatedly.
In these functions, I declare and use some CPLEX Concert objects temporarily, as might be done with "basic C++ objects/types"
(e.g., for comparison, a float-array) in any "basic C++" function.
My question(s) encompass the memory management of IloArray objects, in particular IloNumArray objects, the array class of
Concert's basic floating-point class IloNum, and the need or no need to "delete" IloArray objects (using IloArray::end() method).
Q1: Is an IloNumArray object allocated on the heap (i.e. dynamically), or can it be seen as "just a local floating-point-array"
which - if declared locally in a function - would be deleted once we leave the function? (As the IloArray class rather
resembles the vector class in "basic C++", I suspect the former option).
Q2: If I fail to delete "local IloNumArray" objects in my repeatedly called functions (using IloArray::end()), does this lead
to a risk of memory leakage? (I.e. rougue memory blocks left without associated pointers on the heap?)
I've given an example below, of a situation where I'm led to ask myself the above questions. In the example function
"someFunction(..)", the Concert objects of interest is an IloExpr and an IloNumArray object: tempExpr and tempNumArray, respectively.
I know from Concert C++ examples that deletion of no-longer-used IloExtractables should always be done explicitely.
(If I've understood correctly, the IloExpr object is a subclass of the IloExtractable base class, while the IloNumArray object is not.
Further, an instance of IloExpr is passed by value, and not reference, so "deleting" the expression tempExpr in the end of the
function (tempExpr.end();) wont affect any, say constraints, that has made use of the temporary expression in the function.
However, should I also explicitely delete the IloNumArray tempNumArray, which only have local usage in the function?).
// ------------------------------------------------------------------------------------
// Risk of memory leaks for IloNumArray objects in functions called repeatedly?
// ------------------------------------------------------------------------------------
// libraries
#include <ilcplex/ilocplex.h>
ILOSTLBEGIN
using namespace std;
// function prototype(s)
IloBool someFunction(IloCplex optSolver);
int main (int argc, char **argv)
{
// create the optimization enviroment
IloEnv env;
// no error catching for this simple example.. :)
// create IloModel object associated with the optimization problem
IloModel optModel(env);
/* Add some objective function, constraints and variables to this model.. ... */
// create associated cplex solver object for the model
IloCplex optSolver(optModel);
// now start some loop that will call someFunction() repeatedly (until som
// termination criteria is met)
for (;;) {
// now call someFunction which performs some operations
// on the model and returns "IloTrue" as long as some
// continue criteria is met.
if (! someFunction(optSolver) ) {
env.out() << "Termination criteria met: breaking. " << endl;
break;
}
}
/* print results.. ... */
return 0;
} // END of main()
// FUNCTION: someFunction(...)
IloBool someFunction(IloCplex optSolver) {
// extract optimization enviroment
IloEnv env = optSolver.getEnv();
// declare an IloNumArray object for local use in function
IloNumArray tempNumArray(env);
// declare an IloExpr object for local use in function
IloExpr tempExpr(env);
IloBool someReturnValue;
/* Some operations on the model, using IloNumArray tempNumArray and IloExpr tempExpr.. ... Give value to someReturnValue.. */
// Now, assume we've reached the end of whatever this function is
// for, and that we are about to leave it.
// We clear the memory allocated for temp. expression object tempExpr,
// which is a subclass object of the base class IloExtractable.
tempExpr.end();
// How about the IloNumArray object tempNumArray? Can this be seen
// just as a "local floating point array" that will be deleted once
// we leave the active function? Or is the memory for the tempNumArray
// allocated on the heap, e.g. only the _pointer_ tempNumArray is
// deleted when we leave the function, leaving some rouge memory on
// the heap: possible memory leakage situation?
// tempNumArray.end(); <-- necessary?
return someReturnValue;
} // END of someFunction(...)
#CPLEXOptimizers#DecisionOptimization