From 4eb7a3a5595f27f8631d0361c8724eaec19007b5 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Wed, 29 Jun 2022 11:34:08 +0000 Subject: [PATCH] h264-encoder: Call on_packet_ready last in on_work_done The callback can result in the encoder being destroyed, so we can't dereference access the encoder object after calling it. --- src/h264-encoder.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/h264-encoder.c b/src/h264-encoder.c index a44c771..f6f7217 100644 --- a/src/h264-encoder.c +++ b/src/h264-encoder.c @@ -446,11 +446,20 @@ static void h264_encoder__on_work_done(void* handle) if (self->current_packet.len == 0) return; - self->on_packet_ready(self->current_packet.data, - self->current_packet.len, pts, self->userdata); - vec_clear(&self->current_packet); + void* userdata = self->userdata; + // Must make a copy of packet because the callback might destroy the + // encoder object. + struct vec packet; + vec_init(&packet, self->current_packet.len); + vec_append(&packet, self->current_packet.data, + self->current_packet.len); + + vec_clear(&self->current_packet); h264_encoder__schedule_work(self); + + self->on_packet_ready(packet.data, packet.len, pts, userdata); + vec_destroy(&packet); } static int find_render_node(char *node, size_t maxlen) {