Originally posted by: bermeom
Hello, I am working with ILOG CPLEX Studio 128 in C ++ and I have problems when the API tries to deallocate memory since it never releases and increases. My code consists of a Controller class that it has a description of my MIP using the CPLEX API, and another called ControlSystem class that It has an attribute of type Controller, therefore, ControlSystem calls the Controller's solver method to solve the problem with specific parameters. After a while, it is necessary to update the object controller, then I have explored two possibilities:
-
Update only some problem parameters of the same MIP description, that is, in my particular case, I need to update an IloNumExprArray, which is used in the objective function. But with the following code the memoery of the old expression not are released
class Controller{
IloEnv env;
IloNumExprArray c;
setup(){
c = IloExprArray(this->env);
...
}
update(..){
c.end(); // memory is not released
c = IloExprArray(this->env);// and increases when a new one is created
..
}
}
-
Therefore, my second solution is using two pointer of Controller class when it is necessary to update, the second pointer will create a new controller and then be exchanged. Thus, the old controller object is deleted. This solution works sometimes and sometimes not, so after a while, the program consumes all the memory of my computer. This is an example of my code:
class ControllerSystem{
Controller controller1*;
Controller controller2*;
ControllerSystem(..){
this->controller1 = new Controller(..);
this->controller2 = NULL;
..
}
updateController(..){
this->controller2 = new Controller(..);
Controller aux = this->controller1;
this->controller1 = controller2;
this->controller2 = aux;
delete this->controller2;
this->controller2 = NULL;
}
...
}
class Controller {
IloEnv env;
IloModel model;
IloCplex cplex;
Controller(..){
..
setup();
}
~Controller(..){
this->cplex.end();
this->model.end();
this->env.end();
}
}
Some forums recommend using Valgrind or any tool to detect these types of memory problems. Therefore, I used Valgrind and got these results:
class Controller {
IloExtractableArray refConstraints;
..
void setup(..){
..
this->refConstraints.add(this->model.add(IloMinimize(this->env, IloScalProd(this->c,this->vars) )));
// This line produce the following Valgrind output:
==25031== Warning: set address range perms: large range [0xdc2ffe70, 0xeef3dca0) (undefined)
==25031== Warning: set address range perms: large range [0x93644028, 0xa6281e88) (noaccess)
==25031== Warning: set address range perms: large range [0x101b7be70, 0x1147b9ca0) (undefined)
==25031== Warning: set address range perms: large range [0xa6282028, 0xb8ebfe88) (noaccess)
..
this->cplex = IloCplex(this->model);// This line produce the following Valgrind output:
==25031== Warning: set address range perms: large range [0x9364a040, 0xa6281e70) (undefined)
==25031== Warning: set address range perms: large range [0xa6288040, 0xb8ebfe70) (undefined)
}
..
}
So, I would like to know what is the correct way to eliminate a memory problem. I know that memory is not deleted immediately after using env.end (); but I give the program time to free the memory and that never happens, it just increases.
#CPLEXOptimizers#DecisionOptimization