webmcp
diff libraries/mondelefant/mondelefant_native.c @ 410:92406e7b25cf
Replaced mondelefant_cleanup function with local cleanup code
author | jbe |
---|---|
date | Thu Jan 07 01:44:38 2016 +0100 (2016-01-07) |
parents | ebe9416db4e0 |
children | 7d43be9afa56 |
line diff
1.1 --- a/libraries/mondelefant/mondelefant_native.c Thu Jan 07 01:26:11 2016 +0100 1.2 +++ b/libraries/mondelefant/mondelefant_native.c Thu Jan 07 01:44:38 2016 +0100 1.3 @@ -34,18 +34,6 @@ 1.4 #define MONDELEFANT_SERVER_ENCODING_ASCII 0 1.5 #define MONDELEFANT_SERVER_ENCODING_UTF8 1 1.6 1.7 -// hack to avoid cumulating memory leaks in case of out-of-memory errors 1.8 -static void mondelefant_cleanup(mondelefant_conn_t *conn) { 1.9 - if (conn->todo_PQfreemem) { 1.10 - PQfreemem(conn->todo_PQfreemem); 1.11 - conn->todo_PQfreemem = NULL; 1.12 - } 1.13 - if (conn->todo_PQclear) { 1.14 - PQclear(conn->todo_PQclear); 1.15 - conn->todo_PQclear = NULL; 1.16 - } 1.17 -} 1.18 - 1.19 // transform codepoint-position to byte-position for a given UTF-8 string: 1.20 static size_t utf8_position_to_byte(const char *str, size_t utf8pos) { 1.21 size_t bytepos; 1.22 @@ -379,9 +367,18 @@ 1.23 static int mondelefant_conn_free(lua_State *L) { 1.24 mondelefant_conn_t *conn; 1.25 conn = luaL_checkudata(L, 1, MONDELEFANT_CONN_MT_REGKEY); 1.26 - mondelefant_cleanup(conn); 1.27 - if (conn->pgconn) PQfinish(conn->pgconn); 1.28 - conn->pgconn = NULL; 1.29 + if (conn->todo_PQfreemem) { 1.30 + PQfreemem(conn->todo_PQfreemem); 1.31 + conn->todo_PQfreemem = NULL; 1.32 + } 1.33 + if (conn->todo_PQclear) { 1.34 + PQclear(conn->todo_PQclear); 1.35 + conn->todo_PQclear = NULL; 1.36 + } 1.37 + if (conn->pgconn) { 1.38 + PQfinish(conn->pgconn); 1.39 + conn->pgconn = NULL; 1.40 + } 1.41 return 0; 1.42 } 1.43 1.44 @@ -614,7 +611,10 @@ 1.45 // get second argument, which must be a string: 1.46 input = luaL_checklstring(L, 2, &input_len); 1.47 // avoid cumulating memory leaks in case of previous out-of-memory errors: 1.48 - mondelefant_cleanup(conn); 1.49 + if (conn->todo_PQfreemem) { 1.50 + PQfreemem(conn->todo_PQfreemem); 1.51 + conn->todo_PQfreemem = NULL; 1.52 + } 1.53 // call PQescapeByteaConn, which allocates memory itself: 1.54 output = (char *)PQescapeByteaConn( 1.55 conn->pgconn, (const unsigned char *)input, input_len, &output_len 1.56 @@ -628,7 +628,7 @@ 1.57 return luaL_error(L, "Could not allocate memory for binary quoting."); 1.58 } 1.59 } 1.60 - // ensure call of PQfreemem in case of unexpected out-of-memory error: 1.61 + // ensure call of PQfreemem in case of repeated out-of-memory errors: 1.62 conn->todo_PQfreemem = output; 1.63 // create Lua string enclosed by single quotes: 1.64 luaL_buffinit(L, &buf); 1.65 @@ -935,7 +935,10 @@ 1.66 // if PQsendQuery call was successful, then fetch result data: 1.67 if (sent_success) { 1.68 // avoid cumulating memory leaks in case of previous out-of-memory errors: 1.69 - mondelefant_cleanup(conn); 1.70 + if (conn->todo_PQclear) { 1.71 + PQclear(conn->todo_PQclear); 1.72 + conn->todo_PQclear = NULL; 1.73 + } 1.74 // NOTE: PQgetResult called one extra time. Break only, if all 1.75 // queries have been processed and PQgetResult returned NULL. 1.76 res = PQgetResult(conn->pgconn); 1.77 @@ -944,7 +947,7 @@ 1.78 pgstatus = PQresultStatus(res); 1.79 rows = PQntuples(res); 1.80 cols = PQnfields(res); 1.81 - // ensure eventual call of PQclear in case of unexpected Lua errors: 1.82 + // ensure call of PQclear in case of repeated Lua errors: 1.83 conn->todo_PQclear = res; 1.84 } 1.85 }