# HG changeset patch # User jbe # Date 1452127478 -3600 # Node ID 92406e7b25cff6f1e692078bc3bc9ee9e2f8c0c9 # Parent ebe9416db4e01a3063a244d7ac9f963f77d4c0b1 Replaced mondelefant_cleanup function with local cleanup code diff -r ebe9416db4e0 -r 92406e7b25cf libraries/mondelefant/mondelefant_native.c --- a/libraries/mondelefant/mondelefant_native.c Thu Jan 07 01:26:11 2016 +0100 +++ b/libraries/mondelefant/mondelefant_native.c Thu Jan 07 01:44:38 2016 +0100 @@ -34,18 +34,6 @@ #define MONDELEFANT_SERVER_ENCODING_ASCII 0 #define MONDELEFANT_SERVER_ENCODING_UTF8 1 -// hack to avoid cumulating memory leaks in case of out-of-memory errors -static void mondelefant_cleanup(mondelefant_conn_t *conn) { - if (conn->todo_PQfreemem) { - PQfreemem(conn->todo_PQfreemem); - conn->todo_PQfreemem = NULL; - } - if (conn->todo_PQclear) { - PQclear(conn->todo_PQclear); - conn->todo_PQclear = NULL; - } -} - // transform codepoint-position to byte-position for a given UTF-8 string: static size_t utf8_position_to_byte(const char *str, size_t utf8pos) { size_t bytepos; @@ -379,9 +367,18 @@ static int mondelefant_conn_free(lua_State *L) { mondelefant_conn_t *conn; conn = luaL_checkudata(L, 1, MONDELEFANT_CONN_MT_REGKEY); - mondelefant_cleanup(conn); - if (conn->pgconn) PQfinish(conn->pgconn); - conn->pgconn = NULL; + if (conn->todo_PQfreemem) { + PQfreemem(conn->todo_PQfreemem); + conn->todo_PQfreemem = NULL; + } + if (conn->todo_PQclear) { + PQclear(conn->todo_PQclear); + conn->todo_PQclear = NULL; + } + if (conn->pgconn) { + PQfinish(conn->pgconn); + conn->pgconn = NULL; + } return 0; } @@ -614,7 +611,10 @@ // get second argument, which must be a string: input = luaL_checklstring(L, 2, &input_len); // avoid cumulating memory leaks in case of previous out-of-memory errors: - mondelefant_cleanup(conn); + if (conn->todo_PQfreemem) { + PQfreemem(conn->todo_PQfreemem); + conn->todo_PQfreemem = NULL; + } // call PQescapeByteaConn, which allocates memory itself: output = (char *)PQescapeByteaConn( conn->pgconn, (const unsigned char *)input, input_len, &output_len @@ -628,7 +628,7 @@ return luaL_error(L, "Could not allocate memory for binary quoting."); } } - // ensure call of PQfreemem in case of unexpected out-of-memory error: + // ensure call of PQfreemem in case of repeated out-of-memory errors: conn->todo_PQfreemem = output; // create Lua string enclosed by single quotes: luaL_buffinit(L, &buf); @@ -935,7 +935,10 @@ // if PQsendQuery call was successful, then fetch result data: if (sent_success) { // avoid cumulating memory leaks in case of previous out-of-memory errors: - mondelefant_cleanup(conn); + if (conn->todo_PQclear) { + PQclear(conn->todo_PQclear); + conn->todo_PQclear = NULL; + } // NOTE: PQgetResult called one extra time. Break only, if all // queries have been processed and PQgetResult returned NULL. res = PQgetResult(conn->pgconn); @@ -944,7 +947,7 @@ pgstatus = PQresultStatus(res); rows = PQntuples(res); cols = PQnfields(res); - // ensure eventual call of PQclear in case of unexpected Lua errors: + // ensure call of PQclear in case of repeated Lua errors: conn->todo_PQclear = res; } }