Skip to content

Commit

Permalink
Fix memory leak when VNC exit
Browse files Browse the repository at this point in the history
- Register client cleanup function when during initialization to ensure
that twin_peer_t is freed properly when the client disconnects.
- Properly release resources during VNC server shutdown by unreferencin
current_fb and free the framebuffer allocated for tx->framebuffer

Close #76
  • Loading branch information
ndsl7109256 committed Dec 23, 2024
1 parent 16f7f9e commit ee8a00a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions apps/multi.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ static void apps_flower_start(twin_screen_t *screen, int x, int y, int w, int h)
draw_flower(path, D(3), 5);
twin_paint_path(pixmap, 0xffe2d2d2, path);
twin_path_destroy(stroke);
twin_path_destroy(path);
twin_window_show(window);
}

Expand Down
11 changes: 10 additions & 1 deletion backend/vnc.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,18 @@ static bool _twin_vnc_work(void *closure)
return true;
}

static void _twin_vnc_client_cleanup(struct nvnc_client *client)
{
twin_peer_t *peer = nvnc_get_userdata(client);
free(peer);
nvnc_set_userdata(client, NULL, NULL);
}

static void _twin_vnc_new_client(struct nvnc_client *client)
{
twin_peer_t *peer = malloc(sizeof(twin_peer_t));
nvnc_set_userdata(client, peer, NULL);
nvnc_set_client_cleanup_fn(client, _twin_vnc_client_cleanup);
}

static void _twin_vnc_pointer_event(struct nvnc_client *client,
Expand Down Expand Up @@ -268,11 +276,12 @@ static void twin_vnc_exit(twin_context_t *ctx)
return;

twin_vnc_t *tx = PRIV(ctx);

nvnc_fb_unref(tx->current_fb);
nvnc_display_unref(tx->display);
nvnc_close(tx->server);
aml_unref(tx->aml);

free(tx->framebuffer);
free(ctx->priv);
free(ctx);
}
Expand Down

0 comments on commit ee8a00a

Please sign in to comment.