@@ -294,11 +294,17 @@ Py::Object _path_module::get_path_extents(const Py::Tuple& args)
294294 agg::trans_affine trans = py_to_agg_transformation_matrix (args[1 ], false );
295295
296296 npy_intp extent_dims[] = { 2 , 2 , 0 };
297- double * extents_data = new double [ 4 ] ;
297+ double * extents_data = NULL ;
298298 double xm, ym;
299299 PyArrayObject* extents = NULL ;
300300 try
301301 {
302+ extents = (PyArrayObject*)PyArray_SimpleNew
303+ (2 , extent_dims, PyArray_DOUBLE);
304+ if (extents == NULL )
305+ throw Py::MemoryError (" Could not allocate result array" );
306+ extents_data = (double *)PyArray_DATA (extents);
307+
302308 extents_data[0 ] = std::numeric_limits<double >::infinity ();
303309 extents_data[1 ] = std::numeric_limits<double >::infinity ();
304310 extents_data[2 ] = -std::numeric_limits<double >::infinity ();
@@ -307,16 +313,10 @@ Py::Object _path_module::get_path_extents(const Py::Tuple& args)
307313 ::get_path_extents (path, trans,
308314 &extents_data[0 ], &extents_data[1 ], &extents_data[2 ], &extents_data[3 ],
309315 &xm, &ym);
310-
311- extents = (PyArrayObject*)PyArray_SimpleNewFromData
312- (2 , extent_dims, PyArray_DOUBLE, extents_data);
313316 }
314317 catch (...)
315318 {
316- if (extents)
317- Py_XDECREF (extents);
318- else
319- delete[] extents_data;
319+ Py_XDECREF (extents);
320320 throw ;
321321 }
322322
@@ -357,15 +357,27 @@ Py::Object _path_module::update_path_extents(const Py::Tuple& args)
357357 Py_XDECREF (input_minpos);
358358
359359 npy_intp extent_dims[] = { 2 , 2 , 0 };
360- double * extents_data = new double [ 4 ] ;
360+ double * extents_data = NULL ;
361361 npy_intp minpos_dims[] = { 2 , 0 };
362- double * minpos_data = new double [ 2 ] ;
362+ double * minpos_data = NULL ;
363363 PyArrayObject* extents = NULL ;
364364 PyArrayObject* minpos = NULL ;
365365 bool changed = false ;
366366
367367 try
368368 {
369+ extents = (PyArrayObject*)PyArray_SimpleNew
370+ (2 , extent_dims, PyArray_DOUBLE);
371+ if (extents == NULL )
372+ throw Py::MemoryError (" Could not allocate result array" );
373+ minpos = (PyArrayObject*)PyArray_SimpleNew
374+ (1 , minpos_dims, PyArray_DOUBLE);
375+ if (minpos == NULL )
376+ throw Py::MemoryError (" Could not allocate result array" );
377+
378+ extents_data = (double *)PyArray_DATA (extents);
379+ minpos_data = (double *)PyArray_DATA (minpos);
380+
369381 if (ignore)
370382 {
371383 extents_data[0 ] = std::numeric_limits<double >::infinity ();
@@ -396,21 +408,11 @@ Py::Object _path_module::update_path_extents(const Py::Tuple& args)
396408 minpos_data[0 ] != xm ||
397409 minpos_data[1 ] != ym);
398410
399- extents = (PyArrayObject*)PyArray_SimpleNewFromData
400- (2 , extent_dims, PyArray_DOUBLE, extents_data);
401- minpos = (PyArrayObject*)PyArray_SimpleNewFromData
402- (1 , minpos_dims, PyArray_DOUBLE, minpos_data);
403411 }
404412 catch (...)
405413 {
406- if (extents)
407- Py_XDECREF (extents);
408- else
409- delete[] extents_data;
410- if (minpos)
411- Py_XDECREF (minpos);
412- else
413- delete[] minpos_data;
414+ Py_XDECREF (extents);
415+ Py_XDECREF (minpos);
414416 throw ;
415417 }
416418
@@ -419,6 +421,9 @@ Py::Object _path_module::update_path_extents(const Py::Tuple& args)
419421 result[1 ] = Py::Object ((PyObject*) minpos);
420422 result[2 ] = Py::Int (changed ? 1 : 0 );
421423
424+ Py_XDECREF (extents);
425+ Py_XDECREF (minpos);
426+
422427 return result;
423428}
424429
@@ -964,7 +969,7 @@ Py::Object _path_module::count_bboxes_overlapping_bbox(const Py::Tuple& args)
964969{
965970 args.verify_length (2 );
966971
967- Py::Object bbox = args[0 ];
972+ Py::Object bbox = args[0 ];
968973 Py::SeqBase<Py::Object> bboxes = args[1 ];
969974
970975 double ax0, ay0, ax1, ay1;
@@ -1086,16 +1091,15 @@ void _add_polygon(Py::List& polygons, const std::vector<double>& polygon) {
10861091 if (polygon.size () == 0 )
10871092 return ;
10881093 npy_intp polygon_dims[] = { polygon.size () / 2 , 2 , 0 };
1089- double * polygon_data = new double [polygon.size ()];
1090- memcpy (polygon_data, &polygon[0 ], polygon.size () * sizeof (double ));
10911094 PyArrayObject* polygon_array = NULL ;
1092- polygon_array = (PyArrayObject*)PyArray_SimpleNewFromData
1093- (2 , polygon_dims, PyArray_DOUBLE, polygon_data );
1095+ polygon_array = (PyArrayObject*)PyArray_SimpleNew
1096+ (2 , polygon_dims, PyArray_DOUBLE);
10941097 if (!polygon_array)
10951098 {
1096- delete[] polygon_data;
1097- throw Py::RuntimeError (" Error creating polygon array" );
1099+ throw Py::MemoryError (" Error creating polygon array" );
10981100 }
1101+ double * polygon_data = (double *)PyArray_DATA (polygon_array);
1102+ memcpy (polygon_data, &polygon[0 ], polygon.size () * sizeof (double ));
10991103 polygons.append (Py::Object ((PyObject*)polygon_array));
11001104}
11011105
0 commit comments