From d1c5c14ff7d6b0e215c531f266f5f35197e73660 Mon Sep 17 00:00:00 2001 From: Aaron Griffith Date: Thu, 1 Mar 2012 15:55:47 -0500 Subject: [PATCH] fixed huge memory leak when calling chunk_render on empty section --- overviewer_core/src/iterate.c | 37 +++++++++++++++++++------------- overviewer_core/src/overviewer.h | 2 +- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/overviewer_core/src/iterate.c b/overviewer_core/src/iterate.c index 0f7fe0d..b8791cc 100644 --- a/overviewer_core/src/iterate.c +++ b/overviewer_core/src/iterate.c @@ -187,6 +187,26 @@ int load_chunk(RenderState* state, int x, int z, unsigned char required) { return 0; } +/* helper to unload all loaded chunks */ +static void +unload_all_chunks(RenderState *state) { + unsigned int i, j, k; + for (i = 0; i < 3; i++) { + for (j = 0; j < 3; j++) { + if (state->chunks[i][j].loaded) { + Py_XDECREF(state->chunks[i][j].biomes); + for (k = 0; k < SECTIONS_PER_CHUNK; k++) { + Py_XDECREF(state->chunks[i][j].sections[k].blocks); + Py_XDECREF(state->chunks[i][j].sections[k].data); + Py_XDECREF(state->chunks[i][j].sections[k].skylight); + Py_XDECREF(state->chunks[i][j].sections[k].blocklight); + } + state->chunks[i][j].loaded = 0; + } + } + } +} + unsigned char check_adjacent_blocks(RenderState *state, int x,int y,int z, unsigned char blockid) { /* @@ -484,6 +504,7 @@ chunk_render(PyObject *self, PyObject *args) { /* this section doesn't exist, let's skeddadle */ render_mode_destroy(rendermode); Py_DECREF(blockmap); + unload_all_chunks(&state); Py_RETURN_NONE; } @@ -601,21 +622,7 @@ chunk_render(PyObject *self, PyObject *args) { render_mode_destroy(rendermode); Py_DECREF(blockmap); - for (i = 0; i < 3; i++) { - for (j = 0; j < 3; j++) { - if (state.chunks[i][j].loaded) { - int k; - Py_XDECREF(state.chunks[i][j].biomes); - for (k = 0; k < SECTIONS_PER_CHUNK; k++) { - Py_XDECREF(state.chunks[i][j].sections[k].blocks); - Py_XDECREF(state.chunks[i][j].sections[k].data); - Py_XDECREF(state.chunks[i][j].sections[k].skylight); - Py_XDECREF(state.chunks[i][j].sections[k].blocklight); - } - state.chunks[i][j].loaded = 0; - } - } - } + unload_all_chunks(&state); Py_RETURN_NONE; } diff --git a/overviewer_core/src/overviewer.h b/overviewer_core/src/overviewer.h index 89b3c6e..57c7cef 100644 --- a/overviewer_core/src/overviewer.h +++ b/overviewer_core/src/overviewer.h @@ -26,7 +26,7 @@ // increment this value if you've made a change to the c extesion // and want to force users to rebuild -#define OVERVIEWER_EXTENSION_VERSION 26 +#define OVERVIEWER_EXTENSION_VERSION 27 /* Python PIL, and numpy headers */ #include