27.12.2008, 05:25
Quote:
Originally Posted by Syg
Hi,
Concerning the memory leak, I discovered it myself with the first version of this plugin and corrected it. That memory leak is a common one, you allocate memory in a function and return the allocated memory. It's up to the caller to free it. It's very easy to forget to free this memory (that cannot be freed in the called function). And that's exactly what the pcCreateAndFillStringFromCell function does (szDest = new char[nLen+1]. This means that each time you call this function you must call delete on the returned pointer : Example : static cell AMX_NATIVE_CALL n_samp_mysql_query( AMX* amx, cell* params ) { int retval; const char *query; query=pcCreateAndFillStringFromCell(amx, params[1]); retval=mysql_query(&connexion, query); //mysql_query returns 0 if success delete query; // <--- FREE MEMORY ALLOCATED BY pcCreateAndFillStringFromCell AFTER IT IS USED if(!retval) { return 1; } else { logprintf("Error in mysql_query: %s",mysql_error(&connexion)); return 0; } } And for the samp_mysql_strtok function uses the pcCreateAndFillStringFromCell function. So it is normal that Bardokas example uses so much memory. ++ Syg |