Search code examples
linear-programmingscip

Freeing SCIP_SOL* and SCIP* in SCIP optimization software


I initialized the scip environment as follows:

SCIP* scip = nullptr;
SCIP_CALL( SCIPcreate(&scip) );

I created a small problem to learn SCIP and I am storing the solution in

SCIP_SOL* sol = nullptr;
sol = SCIPgetBestSol(scip);

I am trying to free the memory by sol and scip but I am running into segmentation faults.

This leads to segfault:

SCIP_RETCODE retcode =  SCIPfreeSol(scip,&sol);
assert(retcode == SCIP_OKAY);


retcode = SCIPfree(&scip);
assert(retcode == SCIP_OKAY);

If I am not freeing the sol before freeing scip, it works fine and no seg faults occur but I am sure it is not correct. I have checked some other code which comes with SCIP Optimization Suite and some code examples use SCIPfreeSol and some don't, even though they all use SCIP_SOL *sol to define the sol variable.

EDIT: The problem actually works. I did not include the actual model code.


Solution

  • You only need to free the solution if you created it by SCIPcreateSol or similar. When you ask for the best solution via SCIPgetBestSol(scip), this just returns you a pointer to the solution that SCIP created internally. SCIP will also free it, so you don't have to do this yourself. At the same time, since it is not your "own" solution, you should not modify this solution. If you want to do this, you should create your own one or copy the best one via SCIPcreateSolCopy.

    Therefore, freeing only scip is correct in your example.