Search code examples
cmemory-managementsegmentation-faultgstreamerglib

why this program gives segmentation fault heap-use-after-free on address


Program tries to create multiple webrtcbin elements to receive media from different sources same program works fine on windows but in mac it gives segmentation fault. on mac if the for loop iterates more than one time then I am getting segmentation fault


static void on_offer_created(GstPromise *promise, gpointer data)
{
  GstWebRTCSessionDescription *offer = NULL;
  const GstStructure *reply;
  gchar *desc;
  gchar *sdp_string;
  gchar *json_string;
  reply = gst_promise_get_reply(promise);
  GstElement *webrtc;
  webrtc = gst_bin_get_by_name(GST_BIN(gst_pipe), (gchar *)data);
  g_assert_nonnull(webrtc);
  gst_structure_get(reply, "offer", GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &offer, NULL);
  gst_promise_unref(promise);

  g_signal_emit_by_name(webrtc, "set-local-description", offer, NULL);

  sdp_string = gst_sdp_message_as_text(offer->sdp);
  g_print(" offer created:\n%s\n", sdp_string);
  JsonObject *sdp_json = json_object_new();
  json_object_set_string_member(sdp_json, "type", "offer");
  json_object_set_string_member(sdp_json, "sdp", sdp_string);
  json_object_set_string_member(sdp_json, "from", ourid);
  json_object_set_string_member(sdp_json, "to", (gchar *)data);

  json_string = get_string_from_json_object(sdp_json);

  soup_websocket_connection_send_text(connection, json_string);
  g_print("sending offer to %s",(gchar *)data);

  gst_webrtc_session_description_free(offer);
}
static void
on_negotiation_needed(GstElement *webrtc, gpointer user_data)
{
  GstPromise *promise;
  g_print("negosiation  needed");
  promise = gst_promise_new_with_change_func(on_offer_created, (gchar *)user_data, NULL);
  g_signal_emit_by_name(webrtc, "create-offer", NULL, promise);
}

  for (int i = json_array_get_length(chain)-1; i >= 0; i--)
  {
    peerid = (gchar *)g_malloc0(sizeof(gchar*)*9);
    webrtc  =(GstElement*)g_malloc0(sizeof(GstElement*));
    peerid=json_array_get_string_element(chain, i);

    webrtc = gst_element_factory_make("webrtcbin", peerid);
    g_assert(webrtc != NULL);

    gst_bin_add_many(GST_BIN(gst_pipe), webrtc, NULL);
    GstCaps *caps = gst_caps_from_string(TRANS_AUDIO_CAPS);
    g_signal_emit_by_name(webrtc, "add-transceiver", GST_WEBRTC_RTP_TRANSCEIVER_DIRECTION_RECVONLY, caps, NULL);

    g_signal_connect(webrtc, "pad-added", G_CALLBACK(on_incoming_stream), &peer_delay);
    g_signal_connect(webrtc, "on-negotiation-needed", G_CALLBACK(on_negotiation_needed),g_strdup (peerid));
    g_signal_connect(webrtc, "on-ice-candidate", G_CALLBACK(send_ice_candidate_message), g_strdup (peerid));
    g_assert_true(gst_element_sync_state_with_parent(webrtc));

  }


Solution

  • I was using libsoup for socket connection. It was failing I have changed the library and It works fine now