Language: C
Label: safe
CWE: CWE-416 \u2014 Use After Free
Referencing memory after it has been freed can cause a program to crash, use unexpected values, or execute code.

Code:
static GF_Err mp4_mux_configure_pid(GF_Filter *filter, GF_FilterPid *pid, Bool is_remove)
{
	GF_MP4MuxCtx *ctx = gf_filter_get_udta(filter);

	if (is_remove) {
		TrackWriter *tkw = gf_filter_pid_get_udta(pid);
		if (tkw) {
			gf_list_del_item(ctx->tracks, tkw);
			if (ctx->ref_tkw == tkw) ctx->ref_tkw = gf_list_get(ctx->tracks, 0);
			gf_free(tkw);
		}
		//removing last pid
		if (ctx->opid && !gf_list_count(ctx->tracks)) {
			if (ctx->file) {
				//non-frag file, flush file
				if (!ctx->init_movie_done) {
					mp4_mux_done(ctx, GF_TRUE);
				}
			} else {
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
				while (ctx->flush_size) {
					GF_Err e = mp4_mux_flush_fragmented(ctx);
					if (e) break;
				}
#endif
			}
			//delete output pid (to flush destruction of filter chain)
			gf_filter_pid_remove(ctx->opid);
			ctx->opid = NULL;
		}
		return GF_OK;
	}
	return mp4_mux_setup_pid(filter, pid, GF_TRUE);
}