@@ -139,11 +139,6 @@ static PyObject *Py_write_png(PyObject *self, PyObject *args, PyObject *kwds)
139
139
numpy::array_view<unsigned char , 3 > buffer;
140
140
PyObject *filein;
141
141
PyObject *metadata = NULL ;
142
- PyObject *meta_key, *meta_val;
143
- png_text *text;
144
- Py_ssize_t pos = 0 ;
145
- int meta_pos = 0 ;
146
- Py_ssize_t meta_size;
147
142
double dpi = 0 ;
148
143
int compression = 6 ;
149
144
int filter = -1 ;
@@ -274,15 +269,21 @@ static PyObject *Py_write_png(PyObject *self, PyObject *args, PyObject *kwds)
274
269
PyErr_SetString (PyExc_TypeError, " metadata must be a dict or None" );
275
270
goto exit;
276
271
}
277
- meta_size = PyDict_Size (metadata);
278
- text = new png_text[meta_size];
272
+
273
+ Py_ssize_t meta_size = PyDict_Size (metadata);
274
+ png_text *text = new png_text[meta_size];
275
+ PyObject *meta_key, *meta_val;
276
+ Py_ssize_t pos = 0 ;
277
+ int meta_pos = 0 ;
278
+ std::vector<PyObject*> temp_strs;
279
279
280
280
while (PyDict_Next (metadata, &pos, &meta_key, &meta_val)) {
281
281
text[meta_pos].compression = PNG_TEXT_COMPRESSION_NONE;
282
282
if (PyUnicode_Check (meta_key)) {
283
283
PyObject *temp_key = PyUnicode_AsEncodedString (meta_key, " latin_1" , " strict" );
284
284
if (temp_key != NULL ) {
285
285
text[meta_pos].key = PyBytes_AsString (temp_key);
286
+ temp_strs.push_back (temp_key);
286
287
}
287
288
} else if (PyBytes_Check (meta_key)) {
288
289
text[meta_pos].key = PyBytes_AsString (meta_key);
@@ -295,6 +296,7 @@ static PyObject *Py_write_png(PyObject *self, PyObject *args, PyObject *kwds)
295
296
PyObject *temp_val = PyUnicode_AsEncodedString (meta_val, " latin_1" , " strict" );
296
297
if (temp_val != NULL ) {
297
298
text[meta_pos].text = PyBytes_AsString (temp_val);
299
+ temp_strs.push_back (temp_val);
298
300
}
299
301
} else if (PyBytes_Check (meta_val)) {
300
302
text[meta_pos].text = PyBytes_AsString (meta_val);
@@ -307,6 +309,10 @@ static PyObject *Py_write_png(PyObject *self, PyObject *args, PyObject *kwds)
307
309
meta_pos++;
308
310
}
309
311
png_set_text (png_ptr, info_ptr, text, meta_size);
312
+ for (std::vector<PyObject*>::iterator it = temp_strs.begin ();
313
+ it != temp_strs.end (); ++it) {
314
+ Py_DECREF (*it);
315
+ }
310
316
delete[] text;
311
317
}
312
318
#endif
0 commit comments