[SCIP] Multiple SCIP_ConsData cause heap corruption
Hendel, Gregor
hendel at zib.de
Thu Oct 22 11:19:26 CEST 2020
Hi Marko,
Marc says off-topic, but I won't erase my answer now 😊
I admit I haven't looked at your minimal working example. In the C world, your easiest option is to introduce distinct structs for each type in the headers
cons_one.h: struct one_data {...}
cons_two.h: struct two_data {...}
...
and to create a constraint data using
struct one_data *mydata;
mydata = malloc(sizeof(struct one_data));
// constraint creation
SCIPsetConsData(scip, cons_of_type_one, (SCIP_CONSDATA*)mydata);
Whenever the constraint wants to use its above data in the implementation in one.cpp, cast it back to its original type:
struct one_data *mydata = (struct one_data *)SCIPconsGetData(cons);
Note that for all constraint handlers of SCIP, their data implementation is entirely private to the c file, they *NEVER* expose their internal data structure of struct SCIP_ConsData to the world outside. This is why you pass a list of weights, list of binary variables, and a capacity to the Knapsack constraint handler at constraint creation time, but not an object of SCIP_KNAPSACKDATA type, which does not even exist.
Now it's time for C++ guys to tell you a different approach.
Cheers,
Gregor
________________________________
Von: Scip <scip-bounces at zib.de> im Auftrag von Horváth Markó <marko.horvath at sztaki.hu>
Gesendet: Donnerstag, 22. Oktober 2020 10:37:55
An: scip at zib.de
Betreff: [SCIP] Multiple SCIP_ConsData cause heap corruption
Dear All,
I am working on a branch-and-price project using SCIP, but I got stuck
on something. Actually, I reduced my problem to a "simple" C++ issue,
but due to the design of SCIP maybe you have already run into this
problem and know the solution.
So, my problem is that I have three different user-defined constraints
and thus three different user-defined constraint data, that is, three
different structures in three different .cpp files with the same name:
SCIP_ConsData. (Due to the design of SCIP, I must use this name for all
structures). Everything was fine when I used only the first two
constraints (it was also a luck, I guess), however, the third one ruined
everything. The problem is that in runtime these structures are mixed up
which causes a heap corruption.
More precisely, I have three SCIP_ConsData structures in different .cpp
files (say, one.cpp, two.cpp, three.cpp) and when I want to create a new
instance of the corresponding SCIP_ConsData in file two.cpp, the
structure in file one.cpp is going to be created. Later, it will cause a
heap corruption.
What should I do to fix this issue?
I attach a very minimal working example for this phenomenon. I use
Visual Studio 2019 under Windows 10 with C++17 Standard.
Best Regards,
Marko
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listserv.zib.de/pipermail/scip/attachments/20201022/52f7b09d/attachment.html>
More information about the Scip
mailing list