Skip to content

Commit e81b044

Browse files
committedDec 2, 2012
fix segfaults and memory leaks in sip files (fix #6805)
1 parent 5e8e4ae commit e81b044

File tree

1 file changed

+44
-17
lines changed

1 file changed

+44
-17
lines changed
 

‎python/core/conversions.sip

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ template <TYPE>
103103

104104
if (*sipIsErr)
105105
{
106-
sipReleaseInstance(t, sipClass_TYPE, state);
106+
sipReleaseMappedType(t, qvector_qgspoint, state);
107107
delete ql;
108108
return 0;
109109
}
110110
ql->append(*t);
111-
sipReleaseInstance(t, sipClass_TYPE, state);
111+
sipReleaseMappedType(t, qvector_qgspoint, state);
112112
}
113113

114114
*sipCppPtr = ql;
@@ -185,12 +185,12 @@ template <TYPE>
185185

186186
if (*sipIsErr)
187187
{
188-
sipReleaseInstance(t, sipClass_TYPE, state);
188+
sipReleaseMappedType(t, qvector_qgspoint, state);
189189
delete ql;
190190
return 0;
191191
}
192192
ql->append(*t);
193-
sipReleaseInstance(t, sipClass_TYPE, state);
193+
sipReleaseMappedType(t, qvector_qgspoint, state);
194194
}
195195

196196
*sipCppPtr = ql;
@@ -261,12 +261,12 @@ template <TYPE>
261261

262262
if (*sipIsErr)
263263
{
264-
sipReleaseInstance(t, sipClass_TYPE, state);
264+
sipReleaseMappedType(t, qlist_type, state);
265265
delete ql;
266266
return 0;
267267
}
268268
ql->append(*t);
269-
sipReleaseInstance(t, sipClass_TYPE, state);
269+
sipReleaseMappedType(t, qlist_type, state);
270270
}
271271

272272
*sipCppPtr = ql;
@@ -571,38 +571,63 @@ template<TYPE>
571571

572572
%ConvertToTypeCode
573573
PyObject *kobj, *tobj, *kobj2, *tobj2;
574+
Py_ssize_t i = 0;
575+
576+
//TODO: it works using SIP
577+
#if (SIP_VERSION >= 0x041200)
578+
const sipMappedType* qmap2 = sipFindMappedType("QMap<int, TYPE>");
579+
#endif
574580

575581
// Check the type if that is all that is required.
576582
if (sipIsErr == NULL)
577583
{
578584
if (!PyDict_Check(sipPy))
579585
return 0;
580586

581-
Py_ssize_t i = 0;
582587
while (PyDict_Next(sipPy, &i, &kobj, &tobj))
583588
{
584589
if (!PyDict_Check(tobj))
585590
return 0;
586591

592+
#if (SIP_VERSION >= 0x041200)
593+
if (!sipCanConvertToMappedType(tobj, qmap2, SIP_NOT_NONE))
594+
return 0;
595+
#else
587596
Py_ssize_t j = 0;
588597
while (PyDict_Next(tobj, &j, &kobj2, &tobj2))
589598
{
590599
if (!sipCanConvertToInstance(tobj2, sipClass_TYPE, SIP_NOT_NONE))
591600
return 0;
592601
}
593-
602+
#endif
594603
}
595604
return 1;
596605
}
597606

598607
QMap<qint64, QMap<int, TYPE> > *qm = new QMap<qint64, QMap<int, TYPE> >;
599608

600-
601-
Py_ssize_t i = 0;
602609
while (PyDict_Next(sipPy, &i, &kobj, &tobj))
603610
{
604611
qint64 k = PyLong_AsLongLong(kobj);
605612

613+
#if (SIP_VERSION >= 0x041200)
614+
// TODO: search for the minimum SIP version this code works on, it works
615+
// on SIP 4.13.3 (GS). See #else to know why the version check is needed.
616+
617+
int state;
618+
619+
TYPE* t = reinterpret_cast<TYPE*>(sipConvertToMappedType(tobj, qmap2, sipTransferObj,SIP_NOT_NONE,&state,sipIsErr));
620+
621+
if (*sipIsErr)
622+
{
623+
sipReleaseMappedType(t, qmap2, state);
624+
delete qm;
625+
return 0;
626+
}
627+
628+
qm.insert(k, *t);
629+
sipReleaseMappedType(t, qmap2, state);
630+
#else
606631
// using sipConvertToMappedType to convert directly to QMap<int, TYPE> doesn't work
607632
// and ends with a segfault
608633

@@ -614,19 +639,20 @@ template<TYPE>
614639
int k2 = PyInt_AsLong(kobj2);
615640
int state;
616641

617-
TYPE* fa = reinterpret_cast<TYPE*>(sipConvertToInstance(tobj2, sipClass_TYPE, sipTransferObj,SIP_NOT_NONE,&state,sipIsErr));
642+
TYPE* t2 = reinterpret_cast<TYPE*>(sipConvertToInstance(tobj2, sipClass_TYPE, sipTransferObj,SIP_NOT_NONE,&state,sipIsErr));
618643

619644
if (*sipIsErr)
620645
{
621-
sipReleaseInstance(tobj2, sipClass_TYPE, state);
646+
sipReleaseInstance(t2, sipClass_TYPE, state);
622647
delete qm;
623648
return 0;
624649
}
625650

626-
qm2.insert(k2, *fa);
627-
sipReleaseInstance(tobj2, sipClass_TYPE, state);
651+
qm2.insert(k2, *t2);
652+
sipReleaseInstance(t2, sipClass_TYPE, state);
628653
}
629654
qm->insert(k, qm2);
655+
#endif
630656
}
631657

632658
*sipCppPtr = qm;
@@ -703,16 +729,17 @@ template<TYPE>
703729
{
704730
int state;
705731
qint64 k = PyLong_AsLongLong(kobj);
706-
QgsGeometry * fa = reinterpret_cast<QgsGeometry*>(sipConvertToInstance(tobj, sipClass_QgsGeometry, sipTransferObj,SIP_NOT_NONE,&state,sipIsErr));
732+
QgsGeometry * t = reinterpret_cast<QgsGeometry*>(sipConvertToInstance(tobj, sipClass_QgsGeometry, sipTransferObj,SIP_NOT_NONE,&state,sipIsErr));
707733

708734
if (*sipIsErr)
709735
{
710-
sipReleaseInstance(tobj, sipClass_QgsGeometry, state);
736+
sipReleaseInstance(t, sipClass_QgsGeometry, state);
711737
delete qm;
712738
return 0;
713739
}
714740

715-
qm->insert(k, *fa);
741+
qm->insert(k, *t);
742+
sipReleaseInstance(t, sipClass_QgsGeometry, state);
716743
}
717744

718745
*sipCppPtr = qm;

0 commit comments

Comments
 (0)
Please sign in to comment.