- wxCHECK_MSG(length != 0, wxNullImage, wxT("Cannot read XPM from stream of unknown size"));
-
- char *xpm_buffer = new char[length];
- char *p, *q;
- size_t i;
+ wxCHECK_MSG( length != 0, wxNullImage,
+ wxT("Cannot read XPM from stream of unknown size") );
+
+ // use a smart buffer to be sure to free memory even when we return on
+ // error
+ wxCharBuffer buffer(length);
+
+ char *xpm_buffer = (char *)buffer.data();
+ if ( stream.Read(xpm_buffer, length).LastError() == wxSTREAM_READ_ERROR )
+ return wxNullImage;
+ xpm_buffer[length] = '\0';