coin-or / Clp

COIN-OR Linear Programming Solver
Other
396 stars 82 forks source link

Access violation when delete[]ing the array from ClpSimplex::statusCopy() on Win64 #176

Open fschwaiger opened 3 years ago

fschwaiger commented 3 years ago

The documentation for using warm start follows the example in defaults.cpp and goes like this:

ClpSimplex model;
// [...] solve
unsigned char *status = model.statusCopy();
// [...]
ClpSimplex model2 = model;
// [...]
model2.copyinStatus(status);
delete[] status;

I am writing a MEX interface to use CLP in MATLAB and have the following (should-be) equivalent code, that links dynamically agains the official pre-built Clp.dll v1.17.6:

class MexFunction : public matlab::mex::Function
{
unsigned char *status;

public:
    MexFunction() : status(NULL) {}

    ~MexFunction()
    {
        delete[] status;
        status = NULL;
    }

    void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs)
    {
        ClpSimplex *simplex(new ClpSimplex());

        // [ ... ] load problem

        simplex->copyinStatus(status);

        // [ ... ] solve

        delete[] status;
        status = simplex->statusCopy();

        delete simplex;
    }
};

This code is running fine and as expected on Linux and MacOS. On Windows, this code causes Access Violation in the lines with delete[] status. Or, better, it does not crash if I allow the memory leak and comment out the delete[] status lines:

--------------------------------------------------------------------------------
             Access violation detected at 2021-02-16 14:29:12 +0100
--------------------------------------------------------------------------------

...

Stack Trace (from fault):
[  0] 0x00007fffb334024f                      C:\WINDOWS\SYSTEM32\ntdll.dll+00262735 RtlSizeHeap+00000431
[  1] 0x00007fff704e6fd6                   C:\WINDOWS\SYSTEM32\AcLayers.DLL+00028630 NotifyShims+00020230
[  2] 0x00007fffb02914cb                   C:\WINDOWS\System32\ucrtbase.dll+00070859 free_base+00000027
[  3] 0x00007fff1e666f1a                   C:\my-project\clp.mexw64+00225050 MexFunction::operator()+00002378

Now, I remember from a different project that we got Access Violations when writing to global parameters in a different DLL. I understand that according to this MSVC doc article, any access to data from a DLL is forbidden. In my understanding that should include delete[]ing pointers allocated by said DLL.

I think calling delete[] on the return array from statusCopy is wrong as a concept. Maybe one of the following should help?

1) return a std::vector from ClpSimple::statusCopy 2) provide a function in ClpSimplex were the user can return the pointer

```c++
void ClpSimplex::statusFree(unsigned char *status) { delete[] status; }
```
tkralphs commented 3 years ago

I haven't had a chance to look at this, but just wanted to point out that there is a free Matlab toolbox for interfacing to many of the COIN-OR solvers, including Clp: https://www.inverseproblem.co.nz/OPTI/.

fschwaiger commented 3 years ago

Thanks. We are aware of this toolbox and cannot use it, because it is missing this warm start feature.