Ticket #14650 (closed defect: invalid)
wxImage::Scale causes program crash when new size is too large
| Reported by: | Jive Dadson | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | wxMSW | Version: | 2.9-svn |
| Keywords: | Scale wxCHECK_MSG image.cpp GetData crash | Cc: | |
| Blocked By: | Patch: | no | |
| Blocking: |
Description
I have an app with a "+" button that the user can click on to rescale an image larger. If the user clicks it too many times, the program crashes in wxImage::Scale. The library code does not throw an exception that the application code could handle, nor does it return an image for which IsOk() returns false, either of which would be acceptable if documented. Instead it intentionally crashes the application, even in the Release version.
What happens internally is that wxImage::Scale calls wxImage::ResampleNearest, which attempts to make a new image. ResampleNearest then calls GetData on the new image. GetData then invokes the macro wxCHECK_MSG, which crashes the application. Even if it did not, the next line in ResampleNearest would do so, because it is another call to wxCHECK_MSG.
This is a recurring anti-pattern in image.cpp (at least). Where the function should either throw an exception or return a bad image that will return false for IsOk(), it instead crashes the application on purpose.
I put the following work-around in my code:
{ // RAII block
// Work around bug in wxWidgets. Claim as much space
// as Scale will need. Hope the memory survives long enough
// after we free it on exit from this block.
wxImage new_image;
new_image.Create( new_x, new_y, false );
if(!new_image.IsOk()) {
backout();
return;
}
}
