From e85168ac5306e19221ea74538e3f0812e851a322 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Mon, 6 Sep 2021 20:54:44 +0000 Subject: [PATCH] Fixed some memory leaks. Memory leak-related paranoia. Minor cleanups. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3659 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/socket.c | 6 +++++- src/ssb.c | 11 +---------- src/ssb.db.c | 1 + src/ssb.h | 2 -- src/ssb.qjs.c | 17 ++--------------- src/ssb.rpc.h | 4 ++-- src/task.c | 5 +++++ 7 files changed, 16 insertions(+), 30 deletions(-) diff --git a/src/socket.c b/src/socket.c index 3196d4cf..a44936fc 100644 --- a/src/socket.c +++ b/src/socket.c @@ -199,6 +199,7 @@ void _socket_reportError(socket_t* socket, const char* error) { } tf_task_run_jobs(socket->_task); JS_FreeValue(tf_task_get_context(socket->_task), exception); + JS_FreeValue(tf_task_get_context(socket->_task), result); } else { fprintf(stderr, "Socket::reportError: %s\n", error); } @@ -551,6 +552,7 @@ void _socket_onRead(uv_stream_t* stream, ssize_t readSize, const uv_buf_t* buffe printf("Socket error on read.\n"); js_std_dump_error(tf_task_get_context(socket->_task)); } + JS_FreeValue(tf_task_get_context(socket->_task), result); tf_task_run_jobs(socket->_task); } _socket_close_internal(socket); @@ -593,6 +595,7 @@ void _socket_onRead(uv_stream_t* stream, ssize_t readSize, const uv_buf_t* buffe printf("Socket error on read plain.\n"); js_std_dump_error(tf_task_get_context(socket->_task)); } + JS_FreeValue(tf_task_get_context(socket->_task), result); tf_task_run_jobs(socket->_task); } break; @@ -631,8 +634,9 @@ void _socket_notifyDataRead(socket_t* socket, const char* data, size_t length) { printf("Socket error on data read.\n"); js_std_dump_error(tf_task_get_context(socket->_task)); } - tf_task_run_jobs(socket->_task); JS_FreeValue(tf_task_get_context(socket->_task), typedArray); + JS_FreeValue(tf_task_get_context(socket->_task), result); + tf_task_run_jobs(socket->_task); } } } diff --git a/src/ssb.c b/src/ssb.c index e98da6c6..63576a71 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -473,13 +473,6 @@ bool tf_ssb_verify_and_strip_signature(JSContext* context, JSValue val, char* ou return verified; } -void tf_ssb_send_createHistoryStream(tf_ssb_t* ssb, const char* id) -{ - for (tf_ssb_connection_t* connection = ssb->connections; connection; connection = connection->next) { - tf_ssb_rpc_send_createHistoryStream(connection, id); - } -} - void tf_ssb_send_close(tf_ssb_t* ssb) { for (tf_ssb_connection_t* connection = ssb->connections; connection; connection = connection->next) { @@ -971,10 +964,8 @@ void tf_ssb_append_message(tf_ssb_t* ssb, JSValue message) JSContext* context = ssb->context; JSValue root = JS_NewObject(context); - JSValue previousstr = JS_NULL; if (have_previous) { - previousstr = JS_NewString(context, previous_id); - JS_SetPropertyStr(context, root, "previous", previousstr); + JS_SetPropertyStr(context, root, "previous", JS_NewString(context, previous_id)); } else { JS_SetPropertyStr(context, root, "previous", JS_NULL); } diff --git a/src/ssb.db.c b/src/ssb.db.c index f9095598..4410fc76 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -310,6 +310,7 @@ static bool _tf_ssb_sqlite_bind_json(JSContext* context, sqlite3* db, sqlite3_st printf("expected string: %s\n", str); JS_FreeCString(context, str); } + JS_FreeValue(context, value); } } else { printf("expected array\n"); diff --git a/src/ssb.h b/src/ssb.h index a3b9e71f..9fe9e4f5 100644 --- a/src/ssb.h +++ b/src/ssb.h @@ -75,8 +75,6 @@ void tf_ssb_connect_str(tf_ssb_t* ssb, const char* address); void tf_ssb_server_open(tf_ssb_t* ssb, int port); void tf_ssb_server_close(tf_ssb_t* ssb); -void tf_ssb_send_createHistoryStream(tf_ssb_t* ssb, const char* id); - void tf_ssb_send_close(tf_ssb_t* ssb); bool tf_ssb_id_str_to_bin(uint8_t* bin, const char* str); diff --git a/src/ssb.qjs.c b/src/ssb.qjs.c index 0bbd53ab..0157c609 100644 --- a/src/ssb.qjs.c +++ b/src/ssb.qjs.c @@ -59,6 +59,7 @@ static JSValue _tf_ssb_blobGet(JSContext* context, JSValueConst this_val, int ar result = JS_NewArrayBufferCopy(context, blob, size); free(blob); } + JS_FreeCString(context, id); } return result; } @@ -98,6 +99,7 @@ static JSValue _tf_ssb_messageContentGet(JSContext* context, JSValueConst this_v result = JS_NewArrayBufferCopy(context, blob, size); free(blob); } + JS_FreeCString(context, id); } return result; } @@ -120,17 +122,6 @@ static JSValue _tf_ssb_connections(JSContext* context, JSValueConst this_val, in return result; } -static JSValue _tf_ssb_createHistoryStream(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) -{ - tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); - if (ssb) { - const char* id = JS_ToCString(context, argv[0]); - tf_ssb_send_createHistoryStream(ssb, id); - JS_FreeCString(context, id); - } - return JS_NULL; -} - static void _check_call(JSContext* context, JSValue result) { if (JS_IsError(context, result)) @@ -385,7 +376,6 @@ void _tf_ssb_on_rpc(tf_ssb_connection_t* connection, uint8_t flags, int32_t requ JSContext* context = tf_ssb_get_context(ssb); JSValue callback = JS_MKPTR(JS_TAG_OBJECT, user_data); JSValue object = JS_NewObject(context); - printf("sending object = %d\n", JS_IsObject(tf_ssb_connection_get_object(connection))); JSValue connection_object = JS_DupValue(context, tf_ssb_connection_get_object(connection)); JS_SetPropertyStr(context, object, "connection", connection_object); JS_SetPropertyStr(context, object, "flags", JS_NewUint32(context, flags)); @@ -462,7 +452,6 @@ static void _tf_ssb_on_blob_want_added(tf_ssb_t* ssb, const char* id, void* user static void _tf_ssb_cleanup_value(tf_ssb_t* ssb, void* user_data) { JSValue callback = JS_MKPTR(JS_TAG_OBJECT, user_data); - printf("CLEANUP %p\n", user_data); JS_FreeValue(tf_ssb_get_context(ssb), callback); } @@ -473,7 +462,6 @@ static JSValue _tf_ssb_register_blob_want_added(JSContext* context, JSValueConst { return JS_ThrowTypeError(context, "Expected argument 1 to be a function."); } - printf("registering %p\n", JS_VALUE_GET_PTR(argv[0])); tf_ssb_register_blob_want_added(ssb, _tf_ssb_on_blob_want_added, _tf_ssb_cleanup_value, JS_VALUE_GET_PTR(JS_DupValue(context, argv[0]))); return JS_UNDEFINED; } @@ -647,7 +635,6 @@ void tf_ssb_init(JSContext* context, tf_ssb_t* ssb) JS_SetPropertyStr(context, object, "blobStore", JS_NewCFunction(context, _tf_ssb_blobStore, "blobStore", 2)); JS_SetPropertyStr(context, object, "messageContentGet", JS_NewCFunction(context, _tf_ssb_messageContentGet, "messageContentGet", 1)); JS_SetPropertyStr(context, object, "connections", JS_NewCFunction(context, _tf_ssb_connections, "connections", 0)); - JS_SetPropertyStr(context, object, "createHistoryStream", JS_NewCFunction(context, _tf_ssb_createHistoryStream, "createHistoryStream", 1)); JS_SetPropertyStr(context, object, "sqlStream", JS_NewCFunction(context, _tf_ssb_sqlStream, "sqlStream", 3)); JS_SetPropertyStr(context, object, "post", JS_NewCFunction(context, _tf_ssb_post, "post", 1)); JS_SetPropertyStr(context, object, "appendMessage", JS_NewCFunction(context, _tf_ssb_appendMessage, "appendMessage", 1)); diff --git a/src/ssb.rpc.h b/src/ssb.rpc.h index ae3a80e3..07d93e42 100644 --- a/src/ssb.rpc.h +++ b/src/ssb.rpc.h @@ -9,6 +9,6 @@ typedef struct _tf_ssb_rpc_t tf_ssb_rpc_t; tf_ssb_rpc_t* tf_ssb_rpc_create(tf_ssb_t* ssb); void tf_ssb_rpc_destroy(tf_ssb_rpc_t* rpc); -void tf_ssb_rpc_send_blobs_get(tf_ssb_connection_t* connection, const char* blob_id, size_t size); +/*void tf_ssb_rpc_send_blobs_get(tf_ssb_connection_t* connection, const char* blob_id, size_t size); void tf_ssb_rpc_send_blobs_createWants(tf_ssb_connection_t* connection); -void tf_ssb_rpc_send_createHistoryStream(tf_ssb_connection_t* connection, const char* id); +void tf_ssb_rpc_send_createHistoryStream(tf_ssb_connection_t* connection, const char* id);*/ diff --git a/src/task.c b/src/task.c index 297d3fa4..25536bcf 100644 --- a/src/task.c +++ b/src/task.c @@ -341,6 +341,7 @@ static void _task_timeoutCallback(uv_timer_t* handle) { 0, NULL); tf_task_report_error(timeout->_task, result); + JS_FreeValue(timeout->_task->_context, result); tf_task_run_jobs(timeout->_task); tf_trace_end(timeout->_task->_trace); free(timeout); @@ -520,8 +521,10 @@ static void _forward_promise(tf_task_t* from, tf_taskstub_t* to, promiseid_t pro JSValue error = JS_Call(from->_context, promise_then, result, 1, &then_handler); tf_task_report_error(from, error); + JS_FreeValue(from->_context, error); error = JS_Call(from->_context, promise_catch, result, 1, &catch_handler); tf_task_report_error(from, error); + JS_FreeValue(from->_context, error); tf_task_run_jobs(from); } @@ -1045,6 +1048,7 @@ void tf_task_resolve_promise(tf_task_t* task, promiseid_t promise, JSValue value tf_task_report_error(task, result); JS_FreeValue(task->_context, it->values[1]); JS_FreeValue(task->_context, it->values[2]); + JS_FreeValue(task->_context, result); _tf_task_free_promise(task, promise); tf_task_run_jobs(task); } else { @@ -1060,6 +1064,7 @@ void tf_task_reject_promise(tf_task_t* task, promiseid_t promise, JSValue value) tf_task_report_error(task, result); JS_FreeValue(task->_context, it->values[1]); JS_FreeValue(task->_context, it->values[2]); + JS_FreeValue(task->_context, result); _tf_task_free_promise(task, promise); tf_task_run_jobs(task); }