Skip to content

Commit bfe3162

Browse files
committed
ANDROID: check for memory allocation errors when resizing the screen
1 parent 65e9ea7 commit bfe3162

File tree

4 files changed

+41
-27
lines changed

4 files changed

+41
-27
lines changed

src/platform/android/jni/display.cpp

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ extern ui::Graphics *graphics;
2727
Canvas::Canvas() :
2828
_w(0),
2929
_h(0),
30-
_pixels(NULL),
31-
_clip(NULL) {
30+
_pixels(nullptr),
31+
_clip(nullptr) {
3232
}
3333

3434
Canvas::~Canvas() {
3535
delete [] _pixels;
3636
delete _clip;
37-
_pixels = NULL;
38-
_clip = NULL;
37+
_pixels = nullptr;
38+
_clip = nullptr;
3939
}
4040

4141
bool Canvas::create(int w, int h) {
@@ -108,16 +108,16 @@ void Canvas::setClip(int x, int y, int w, int h) {
108108
_clip->right = x + w;
109109
_clip->bottom = y + h;
110110
} else {
111-
_clip = NULL;
111+
_clip = nullptr;
112112
}
113113
}
114114

115115
//
116116
// Graphics implementation
117117
//
118118
Graphics::Graphics(android_app *app) : ui::Graphics(),
119-
_fontBuffer(NULL),
120-
_fontBufferB(NULL),
119+
_fontBuffer(nullptr),
120+
_fontBufferB(nullptr),
121121
_app(app),
122122
_w(0),
123123
_h(0),
@@ -129,8 +129,8 @@ Graphics::~Graphics() {
129129

130130
delete [] _fontBuffer;
131131
delete [] _fontBufferB;
132-
_fontBuffer = NULL;
133-
_fontBufferB = NULL;
132+
_fontBuffer = nullptr;
133+
_fontBufferB = nullptr;
134134
}
135135

136136
bool Graphics::construct(int fontId) {
@@ -157,16 +157,16 @@ bool Graphics::construct(int fontId) {
157157
}
158158

159159
void Graphics::redraw() {
160-
if (_app->window != NULL && !_paused) {
160+
if (_app->window != nullptr && !_paused) {
161161
ANativeWindow_Buffer buffer;
162-
bool locked = ANativeWindow_lock(_app->window, &buffer, NULL) == 0;
162+
bool locked = ANativeWindow_lock(_app->window, &buffer, nullptr) == 0;
163163
if (!locked) {
164164
trace("Unable to lock window buffer");
165165
} else {
166166
if (buffer.format != AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM) {
167167
ANativeWindow_unlockAndPost(_app->window);
168168
ANativeWindow_setBuffersGeometry(_app->window, 0, 0, AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM);
169-
locked = ANativeWindow_lock(_app->window, &buffer, NULL) == 0;
169+
locked = ANativeWindow_lock(_app->window, &buffer, nullptr) == 0;
170170
trace("Restore format %d", locked);
171171
}
172172
if (locked) {
@@ -184,11 +184,22 @@ void Graphics::redraw() {
184184
}
185185
}
186186

187-
void Graphics::resize() {
188-
delete _screen;
189-
_screen = new Canvas();
190-
_screen->create(_w, _h);
191-
_drawTarget = NULL;
187+
bool Graphics::resize() {
188+
bool result;
189+
if (_screen == nullptr || _screen->_w != _w || _screen->_h != _h) {
190+
Canvas *newScreen = new Canvas();
191+
result = newScreen->create(_w, _h);
192+
if (result) {
193+
delete _screen;
194+
_screen = newScreen;
195+
_drawTarget = nullptr;
196+
} else {
197+
trace("Failed to resize canvas");
198+
}
199+
} else {
200+
result = true;
201+
}
202+
return result;
192203
}
193204

194205
bool Graphics::loadFonts(int fontId) {

src/platform/android/jni/display.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct Graphics : ui::Graphics {
2424

2525
bool construct(int fontId);
2626
void redraw();
27-
void resize();
27+
bool resize();
2828
void onPaused(bool paused) { _paused=paused; }
2929
void setSize(int w, int h) { _w = w; _h = h; }
3030

src/platform/android/jni/runtime.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -842,8 +842,11 @@ MAEvent Runtime::processEvents(int waitFlag) {
842842
void Runtime::processEvent(MAEvent &event) {
843843
switch (event.type) {
844844
case EVENT_TYPE_SCREEN_CHANGED:
845-
_graphics->resize();
846-
resize();
845+
if (_graphics->resize()) {
846+
resize();
847+
} else {
848+
alert("System error: Failed to resize screen");
849+
}
847850
break;
848851
case EVENT_TYPE_KEY_PRESSED:
849852
handleKeyEvent(event);

src/ui/graphics.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ Font::~Font() {
8181
// Graphics implementation
8282
//
8383
Graphics::Graphics() :
84-
_screen(NULL),
85-
_drawTarget(NULL),
86-
_font(NULL) {
84+
_screen(nullptr),
85+
_drawTarget(nullptr),
86+
_font(nullptr) {
8787
graphics = this;
8888
}
8989

@@ -92,8 +92,8 @@ Graphics::~Graphics() {
9292

9393
FT_Done_FreeType(_fontLibrary);
9494
delete _screen;
95-
graphics = NULL;
96-
_screen = NULL;
95+
graphics = nullptr;
96+
_screen = nullptr;
9797
}
9898

9999
Font *Graphics::createFont(int style, int size) {
@@ -109,7 +109,7 @@ Font *Graphics::createFont(int style, int size) {
109109

110110
void Graphics::deleteFont(Font *font) {
111111
if (font == _font) {
112-
_font = NULL;
112+
_font = nullptr;
113113
}
114114
delete font;
115115
}
@@ -454,7 +454,7 @@ MAHandle Graphics::setDrawTarget(MAHandle maHandle) {
454454
_drawTarget = (Canvas *)maHandle;
455455
}
456456
delete _drawTarget->_clip;
457-
_drawTarget->_clip = NULL;
457+
_drawTarget->_clip = nullptr;
458458
return result;
459459
}
460460

0 commit comments

Comments
 (0)