<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Hello,</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgba(0,0,0,0)">the following mapped type caused the error "free(): double free detected in tcache 2" which I cannot find. Can someone tell me what
I'm missing?</span><br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<pre style="background-color:#ffffff; color:#080808; font-family:'JetBrains Mono',monospace; font-size:9.8pt"><span style="color:rgb(0,0,0); font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; background-color:rgba(0,0,0,0)">PathPoint is a wrapped struct containing some other wrapped structs. The content can also only be an unsigned int and the error is still there.<br><br></span><span style="color:rgb(0,0,0); font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; background-color:rgba(0,0,0,0)"></span></pre>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
When I set
<pre style="background-color:#ffffff; color:#080808; font-family:'JetBrains Mono',monospace; font-size:9.8pt">gc.set_debug(gc.DEBUG_LEAK)</pre>
(meaning postponed garbage collection) in the python script that uses this wrapped vector, the error does not appear.</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
My guess is that it has something to do with "PathPoint *t" or the ownership of "item" in ConvertToTypeCode.<br>
</div>
<span style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)"></span><br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<pre style="background-color:#ffffff; color:#080808; font-family:'JetBrains Mono',monospace; font-size:9.8pt">%MappedType std::vector<PathPoint *><br>{<br>%TypeHeaderCode<br><span style="color:#9e880d">#include </span><span style="color:#067d17"><vector></span><span style="color:#067d17"><br></span>%End<br><br>%ConvertFromTypeCode<br> PyObject *l;<br><br> <span style="color:#0033b3">const </span>sipTypeDef* kpTypeDef = sipFindType(<span style="color:#067d17">"PathPoint"</span>);<br><br> <span style="color:#0033b3">if </span>(!kpTypeDef) {<br> <span style="color:#0033b3">return </span>NULL;<br> }<br><br> <span style="color:#8c8c8c; font-style:italic">// Create the Python list of the correct length.<br></span><span style="color:#8c8c8c; font-style:italic"> </span><span style="color:#0033b3">if </span>((l = PyList_New(sipCpp->size())) == NULL) {<br> <span style="color:#0033b3">return </span>NULL;<br> }<br><br> <span style="color:#0033b3">int </span>i = <span style="color:#1750eb">0</span>;<br> <span style="color:#8c8c8c; font-style:italic">// Go through each element in the C++ instance and convert it to the corresponding Python object.<br></span><span style="color:#8c8c8c; font-style:italic"> </span><span style="color:#0033b3">for</span>(std::vector<PathPoint *>::iterator iter = sipCpp->begin(); iter != sipCpp->end(); iter++) {<br> PathPoint *t = *iter;<br> PyObject *tobj;<br><br> <span style="color:#8c8c8c; font-style:italic">// Get the Python wrapper for the Type instance, creating a new<br></span><span style="color:#8c8c8c; font-style:italic"> // one if necessary, and handle any ownership transfer.<br></span><span style="color:#8c8c8c; font-style:italic"> </span><span style="color:#0033b3">if </span>((tobj = sipConvertFromType(t, kpTypeDef, sipTransferObj)) == NULL) {<br> <span style="color:#8c8c8c; font-style:italic">// There was an error so garbage collect the Python list.<br></span><span style="color:#8c8c8c; font-style:italic"> </span>Py_XDECREF(l);<br> <span style="color:#0033b3">return </span>NULL;<br> }<br><br> <span style="color:#8c8c8c; font-style:italic">// Add the wrapper to the list.<br></span><span style="color:#8c8c8c; font-style:italic"> </span>PyList_SET_ITEM(l, i++, tobj);<br> }<br><br> <span style="color:#8c8c8c; font-style:italic">// Return the Python list.<br></span><span style="color:#8c8c8c; font-style:italic"> </span><span style="color:#0033b3">return </span>l;<br>%End<br><br>%ConvertToTypeCode<br> <span style="color:#0033b3">const </span>sipTypeDef* kpTypeDef = sipFindType(<span style="color:#067d17">"PathPoint"</span>);<br><br> <span style="color:#0033b3">if </span>(!kpTypeDef) {<br> <span style="color:#0033b3">return </span><span style="color:#1750eb">0</span>;<br> }<br><br> <span style="color:#8c8c8c; font-style:italic">// See if we are just being asked to check the type of the Python object.<br></span><span style="color:#8c8c8c; font-style:italic"> </span><span style="color:#0033b3">if </span>(sipIsErr == NULL) {<br> <span style="color:#8c8c8c; font-style:italic">// Check it is a list.<br></span><span style="color:#8c8c8c; font-style:italic"> </span><span style="color:#0033b3">if </span>(!PyList_Check(sipPy)) {<br> <span style="color:#0033b3">return </span><span style="color:#1750eb">0</span>;<br> }<br><br> <span style="color:#8c8c8c; font-style:italic">// Now check each element of the list is of the type we expect.<br></span><span style="color:#8c8c8c; font-style:italic"> // The template is for a pointer type so we don't disallow None.<br></span><span style="color:#8c8c8c; font-style:italic"> </span><span style="color:#0033b3">for </span>(Py_ssize_t i = <span style="color:#1750eb">0</span>; i < PyList_GET_SIZE(sipPy); ++i) {<br> PyObject *item = PyList_GET_ITEM(sipPy, i);<br> <span style="color:#0033b3">if </span>(!sipCanConvertToType(item, kpTypeDef, SIP_NOT_NONE)) {<br> *sipIsErr = <span style="color:#1750eb">1</span>;<br> <span style="color:#0033b3">break</span>;<br> }<br> }<br> <span style="color:#0033b3">return </span><span style="color:#1750eb">1</span>;<br> }<br><br> <span style="color:#8c8c8c; font-style:italic">// Create the instance on the heap.<br></span><span style="color:#8c8c8c; font-style:italic"> </span>std::vector<PathPoint *> *v = <span style="color:#0033b3">new </span>std::vector<PathPoint *>();<br><br> <span style="color:#0033b3">for </span>(Py_ssize_t i = <span style="color:#1750eb">0</span>; i < PyList_GET_SIZE(sipPy); ++i) {<br> <span style="color:#0033b3">int </span>state;<br> <span style="color:#8c8c8c; font-style:italic">// Use the SIP API to convert the Python object to the<br></span><span style="color:#8c8c8c; font-style:italic"> // corresponding C++ instance. Note that we apply any ownership<br></span><span style="color:#8c8c8c; font-style:italic"> // transfer to the list itself, not the individual elements.<br></span><span style="color:#8c8c8c; font-style:italic"> </span>PyObject *item = PyList_GET_ITEM(sipPy, i);<br> PathPoint *t = <span style="color:#0033b3">static_cast</span><PathPoint*>(sipConvertToType(item, kpTypeDef, sipTransferObj,<br> SIP_NOT_NONE, &state, sipIsErr));<br><br> <span style="color:#0033b3">if </span>(*sipIsErr) {<br><br> sipReleaseType(t, kpTypeDef, state);<br><br> Py_DECREF(item);<br><br> <span style="color:#8c8c8c; font-style:italic">// Tidy up.<br></span><span style="color:#8c8c8c; font-style:italic"> </span><span style="color:#0033b3">delete </span>v;<br><br> <span style="color:#8c8c8c; font-style:italic">// There is nothing on the heap.<br></span><span style="color:#8c8c8c; font-style:italic"> </span><span style="color:#0033b3">return </span><span style="color:#1750eb">0</span>;<br> }<br><br> <span style="color:#8c8c8c; font-style:italic">// ownership to cpp (no Py_DECREF needed)<br></span><span style="color:#8c8c8c; font-style:italic"> </span>sipTransferTo(item, item);<br><br> <span style="color:#8c8c8c; font-style:italic">// Add the pointer to the C++ instance.<br></span><span style="color:#8c8c8c; font-style:italic"> </span>v->push_back(t);<br><br> sipReleaseType(t, kpTypeDef, state);<br> }<br><br> <span style="color:#8c8c8c; font-style:italic">// Return the instance on the heap.<br></span><span style="color:#8c8c8c; font-style:italic"> </span>*sipCppPtr = v;<br><br> <span style="color:#8c8c8c; font-style:italic">// Apply the normal transfer.<br></span><span style="color:#8c8c8c; font-style:italic"> </span><span style="color:#0033b3">return </span>sipGetState(sipTransferObj);<br>%End<br>};</pre>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Thank you!</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Regards</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
M.T.<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
</body>
</html>