0
votes

I want to receive a custom struct of QObject through gpointer on a GstPadProbe callback. I have connected the callback function like this:

struct customData d;

thread = new QThread();

d.worker = new Worker();


d.worker->moveToThread(thread);

d.message = "message\n";

src_pad = gst_element_get_static_pad (osd, "src");
  if (!src_pad)
    g_print ("Unable to get src pad\n");
  else
    gst_pad_add_probe (src_pad, GST_PAD_PROBE_TYPE_BUFFER,
        src_pad_buffer_probe, NULL, NULL);

 g_signal_connect(G_OBJECT(src_pad), "src", 
G_CALLBACK(src_pad_buffer_probe), &d);

My callback function is a probe:

static GstPadProbeReturn src_pad_buffer_probe (GstPad * pad, GstPadProbeInfo * info, gpointer u_data)
{
struct customData *custom = (struct customData *) u_data;
g_print("Hello World!\n%s\n", custom->message);
return GST_PAD_PROBE_OK;
}

I have defined the struct as :

struct customData{
char *message;
Worker *worker;

};

I am trying to access the custom->message on src_pad_buffer_probe() thorough custom struct. But the program crashes unexpectedly. I am experimenting this for GTK and QT multi-threaded communication.

1
Unsure why QObject is discussed but from this code we cannot understand if struct customData d; went out of scope and was destructed at the time of src_pad_buffer_probe() call. It seems to be a simple C/C++ question.Alexander V
@AlexanderV I wonder if g_signal_connect(G_OBJECT(src_pad), "src", G_CALLBACK(src_pad_buffer_probe), &d); is correct or not?neuroSparK

1 Answers

0
votes

Connecting to a signal is just saying "call me later when a certain condition is met". You're passing it a pointer to your structure d you've defined on the stack.

struct customData d;
...
g_signal_connect(G_OBJECT(src_pad), "src", G_CALLBACK(src_pad_buffer_probe), &d);

This means that later on, when the callback is called, d must still exist. This won't be the case if it has been allocated on the stack and you returned from that function. So either declare d as static, or allocate it on the heap through malloc or similar.