@@ -178,16 +178,21 @@ Image::as_str(const Py::Tuple& args) {
178178 const size_t NUMBYTES (rowsOut * colsOut * BPP);
179179 const size_t BPR = colsOut * BPP; // bytes per row
180180
181- agg::int8u buffer[NUMBYTES];
181+ agg::int8u* buffer = new agg::int8u[NUMBYTES];
182+ if (buffer ==NULL ) // todo: also handle allocation throw
183+ throw Py::MemoryError (" Image::as_str could not allocate memory" );
184+
182185 size_t ind=0 ;
183186 for (long rowNum=rowsOut-1 ; rowNum>=0 ; rowNum--) { // not unsigned!
184187 size_t start = rowNum*BPR;
185188 for (size_t j=0 ; j<BPR; j++) {
186189 buffer[ind++] = *(bufferOut + start + j);
187190 }
188191 }
189- return Py::asObject (Py_BuildValue (" lls#" , rowsOut, colsOut,
190- buffer, NUMBYTES));
192+ PyObject* o = Py_BuildValue (" lls#" , rowsOut, colsOut,
193+ buffer, NUMBYTES);
194+ delete [] buffer;
195+ return Py::asObject (o);
191196
192197}
193198
@@ -235,6 +240,9 @@ Image::resize(const Py::Tuple& args) {
235240
236241 size_t NUMBYTES (numrows * numcols * BPP);
237242 agg::int8u *buffer = new agg::int8u[NUMBYTES];
243+ if (buffer ==NULL ) // todo: also handle allocation throw
244+ throw Py::MemoryError (" Image::resize could not allocate memory" );
245+
238246 rbufOut = new agg::rendering_buffer;
239247 rbufOut->attach (buffer, numcols, numrows, numcols * BPP);
240248
@@ -557,6 +565,9 @@ _image_module::from_images(const Py::Tuple& args) {
557565
558566 size_t NUMBYTES (numrows * numcols * imo->BPP );
559567 imo->bufferOut = new agg::int8u[NUMBYTES];
568+ if (imo->bufferOut ==NULL ) // todo: also handle allocation throw
569+ throw Py::MemoryError (" _image_module::from_images could not allocate memory" );
570+
560571 imo->rbufOut = new agg::rendering_buffer;
561572 imo->rbufOut ->attach (imo->bufferOut , imo->colsOut , imo->rowsOut , imo->colsOut * imo->BPP );
562573
@@ -627,6 +638,9 @@ _image_module::fromarray(const Py::Tuple& args) {
627638
628639 size_t NUMBYTES (imo->colsIn * imo->rowsIn * imo->BPP );
629640 agg::int8u *buffer = new agg::int8u[NUMBYTES];
641+ if (buffer==NULL ) // todo: also handle allocation throw
642+ throw Py::MemoryError (" _image_module::fromarray could not allocate memory" );
643+
630644 imo->bufferIn = buffer;
631645 imo->rbufIn = new agg::rendering_buffer;
632646 imo->rbufIn ->attach (buffer, imo->colsIn , imo->rowsIn , imo->colsIn *imo->BPP );
@@ -695,6 +709,9 @@ _image_module::fromarray(const Py::Tuple& args) {
695709 imo->colsOut = imo->colsIn ;
696710
697711 imo->bufferOut = new agg::int8u[NUMBYTES];
712+ if (buffer == imo->bufferOut ) // todo: also handle allocation throw
713+ throw Py::MemoryError (" _image_module::fromarray could not allocate memory" );
714+
698715 imo->rbufOut = new agg::rendering_buffer;
699716 imo->rbufOut ->attach (imo->bufferOut , imo->colsOut , imo->rowsOut , imo->colsOut * imo->BPP );
700717
0 commit comments