unifex seg fault on handle_destroy_state

Hi, i'm implementing g772.1 decoder/encoder plugin and have issue with handle_destroy state. I've taken freeswitch g7221 implementation(https://github.com/traviscross/freeswitch/tree/master/libs/libg722_1/src). I have the following state:
#pragma once
#include <g722_1.h>

typedef struct State State;

struct State {
g722_1_decode_state_t *decode_state;
};

#include "_generated/decoder.h"
#pragma once
#include <g722_1.h>

typedef struct State State;

struct State {
g722_1_decode_state_t *decode_state;
};

#include "_generated/decoder.h"
and added following handle_destroy_state callback:
void handle_destroy_state(UnifexEnv *env, State *state) {
UNIFEX_UNUSED(env);

if (state->decode_state != NULL) {
g722_1_decode_release(state->decode_state);
}
}
void handle_destroy_state(UnifexEnv *env, State *state) {
UNIFEX_UNUSED(env);

if (state->decode_state != NULL) {
g722_1_decode_release(state->decode_state);
}
}
but it causes segmentation fault. g7221_1_decode_release calls free on passed pointer. Implementation of g722_1_decode_release:
int g722_1_decode_release(g722_1_decode_state_t *s)
{
free(s);
return 0;
}
int g722_1_decode_release(g722_1_decode_state_t *s)
{
free(s);
return 0;
}
Is it state freed by unifex itself?
S
spscream•64d ago
I hope this is right place to post unifex questions, since Membrane framework is the owner of unifex.
Q
Qizot•64d ago
Are you sure that you have initialized the state properly (assinging a NULL on initialization or making sure that the state gets initialized before being freed)? Operating with state should have the following steps: 1. unifex_alloc_state to allocate a state 2. unifex_release_state in the same initialization function just before returning from the init function to ensure the state gets released when the last elixir's reference vanishes 3. do any internal cleanup inside of handle_destroy_state, you don't have to release the unifex state inside of the callback, just cleanup after the g722
S
spscream•64d ago
My bad. I made mistake on state initialization, made changes to create:
UNIFEX_TERM create(UnifexEnv *env, int bit_rate, int sample_rate) {
UNIFEX_TERM res;

State *state = unifex_alloc_state(env);
g722_1_decode_state_t decoder_state;
state->decode_state = g722_1_decode_init(&decoder_state, bit_rate, sample_rate);

if (state->decode_state == NULL) {
res = create_result_error(env, "decode_init");
goto exit_create;
}

res = create_result_ok(env, state);
exit_create:
unifex_release_state(env, state);
return res;
}
UNIFEX_TERM create(UnifexEnv *env, int bit_rate, int sample_rate) {
UNIFEX_TERM res;

State *state = unifex_alloc_state(env);
g722_1_decode_state_t decoder_state;
state->decode_state = g722_1_decode_init(&decoder_state, bit_rate, sample_rate);

if (state->decode_state == NULL) {
res = create_result_error(env, "decode_init");
goto exit_create;
}

res = create_result_ok(env, state);
exit_create:
unifex_release_state(env, state);
return res;
}
and now is working correctly. I'm new to C programming, reread about pointers and it seems clear for me now. but may be I fixed it incorrectly, thanks for directions btw final fix:
UNIFEX_TERM create(UnifexEnv *env, int bit_rate, int sample_rate) {
UNIFEX_TERM res;

State *state = unifex_alloc_state(env);

state->decode_state = g722_1_decode_init(NULL, bit_rate, sample_rate);

if (state->decode_state == NULL) {
res = create_result_error(env, "decode_init");
goto exit_create;
}

if (state->decode_state == NULL) {
res = create_result_error(env, "decode_init");
goto exit_create;
}

res = create_result_ok(env, state);
exit_create:
unifex_release_state(env, state);
return res;
}
UNIFEX_TERM create(UnifexEnv *env, int bit_rate, int sample_rate) {
UNIFEX_TERM res;

State *state = unifex_alloc_state(env);

state->decode_state = g722_1_decode_init(NULL, bit_rate, sample_rate);

if (state->decode_state == NULL) {
res = create_result_error(env, "decode_init");
goto exit_create;
}

if (state->decode_state == NULL) {
res = create_result_error(env, "decode_init");
goto exit_create;
}

res = create_result_ok(env, state);
exit_create:
unifex_release_state(env, state);
return res;
}
If I put NULL as initial parameter - g7221_1_decode_init initializes memory itself and it is correctly freed on destroy
Q
Qizot•64d ago
glad it works 🙂
Want results from more Discord servers?
Add your server
More Posts
Developing an advanced Jellyfish use caseHey I've been using jellyfish to develop a platform for essentially one-on-one calls between two peotoilet capacity of outbound_rtx_controllerHi, I'm getting the following error on SessionBin: ``` [error] <0.1282.0>/:sip_rtp/{:outbound_rtx_cOn JF Tracks and Reconnecting (in React)So I noticed a few things about the react-sdk and JF tracks in general. Note I have react code that h264 encoder problemshi guys, I'm using h264 encoder plugin for video encoding and sending it via rtp to client. SometimPipeline for muxing 2 msr files (audio and video) into a single flv fileI have the following pipeline which takes 2 msr files (recorded to disk using the RecordingEntrypoinMP3 output is audible, but test not passHi everyone. I made small changes in `membrane_mp3_lame_plugin` to support other input config (the oGrab keyframe image data from h264 stream?We are looking at some h264 coming from an RTSP stream. Membrane is doing a fine job with the HLS deUnity client?Thanks a lot to the Membrane team. The number of examples and availability of code has been incredibWebRTC stream not workingI've hit an issue trying to get an MPEGTS stream displaying in the browser via WebRTC. All seems toError when compiling free4chatThis is probably pretty basic but I'm at square one. I get an error when I'm compiling free4chat, a Screen ShareIs there any reason why jellyfish_videoroom when is running localy share screen is not working ? I sTesting Membrane ElementI'm trying to setup a simple test of a membrane element, but I'm stumped a bit on how to assert thatClustering and scale out behaviourContext: I've a membrane application running in an elixir cluster of 1. It receives a RTSP stream (URTSP authentication problem?Hi, I am playing with an RTSP camera (**Tapo C210**) that is on the local network. The path to the sExtending the jellyfish video room demo with a queueHey I am curious what a scalable way would be to add a queue in front of jellyfish rooms. I have a sDebugging bundlex/unifex errorsHello, I've been tinkering with membrane cross-compiled to Nerves (rpi4). I've had features (e.g. m