Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
fschwaiger opened this issue Feb 16, 2021 · 2 comments

Comments

@fschwaiger
Copy link

fschwaiger commented Feb 16, 2021

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

    void ClpSimplex::statusFree(unsigned char *status) { delete[] status; }
@tkralphs
Copy link
Member

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants