简体   繁体   English

通过 glib 队列传递数据结构

[英]Passing data structure through glib queue

I have another pointer question, so I'd be really glad if you could help me to solve that我有另一个指针问题,所以如果你能帮我解决这个问题,我会很高兴

I have this structure:我有这个结构:

uint8_t *reconstructed[3];

reconstructed[0] = buff_offset + (uint8_t *) malloc (buff_size);
reconstructed[1] = buff_offset + (uint8_t *) malloc (buff_size);
reconstructed[2] = buff_offset + (uint8_t *) malloc (buff_size);

I use this variable this way:我这样使用这个变量:

y4m_write_frame (fd_out, &ostreaminfo, &oframeinfo, reconstructed);

My task is to paralelize this app, for several reasons I need to put this structure in a GLib queue and work with that after some action.我的任务是并行化这个应用程序,出于几个原因,我需要将此结构放在 GLib 队列中,并在一些操作后使用它。

So I put it in the queue:所以我把它放在队列中:

g_queue_push_tail(queue, (gpointer) reconstructed);

But now I don't know how to get it from there.但现在我不知道如何从那里得到它。 I tried:我试过了:

uint8_t * const * frame = (uint8_t * const *) g_queue_pop_head(queue);
y4m_write_frame (fd_out, &ostreaminfo, &oframeinfo, frame);

But application fails with Segmentation fault.但是应用程序因分段错误而失败。

Can any one help me please?任何人都可以帮助我吗? I don't get the whole pointers problem.我没有得到整个指针问题。

You are pushing memory that is allocated on the stack onto the end of your queue:您将在堆栈上分配的 memory 推到队列的末尾:

uint8_t *reconstructed[3];

And then trying to pull that out of the queue way off somewhere else.然后试图将其从队列中拉到其他地方。 By the time you pull things out of the queue, the space on the stack for your three element reconstructed array is almost certainly being used for something else.当你把东西从队列中拉出来的时候,你的三元素reconstructed数组的堆栈空间几乎肯定会被用于其他东西。 I think you'll have to change reconstructed to a uint8_t ** and allocate it on the heap:我认为您必须将reconstructed更改为uint8_t **并将其分配到堆上:

unint8_t **reconstructed;
reconstructed = malloc(3 * sizeof(uint8_t *));
/* Proceed as before. */

This will keep the reconstructed memory valid (barring other bugs of course) outside the function that declares it.这将使reconstructed的 memory 在声明它的 function 之外保持有效(当然排除其他错误)。 You'll also have to free your reconstructed value (and its elements) when you've pulled it out of the queue and are done with it;当您将其从队列中拉出并完成时,您还必须释放reconstructed的值(及其元素); when freeing, be sure to take into account the odd offsets on the reconstructed elements.释放时,一定要考虑到reconstructed元素上的奇数偏移。

Ok so I've also figured it out I created the wrapper I needed for another reason anyway好的,所以我也发现我创建了我需要的包装器是出于另一个原因

// structure
struct wrapper {
    uint8_t *reconstructed[3];
    int should_reconstruct;
    int run_count;
};

// malloc wrapper
struct wrapper* link = (struct wrapper*) malloc(sizeof(struct wrapper));
link->reconstructed[0] = buff_offset + (uint8_t *) malloc (buff_size);
link->reconstructed[1] = buff_offset + (uint8_t *) malloc (buff_size);
link->reconstructed[2] = buff_offset + (uint8_t *) malloc (buff_size);

// add additional informations to wrapper
link->should_reconstruct = 0;
link->run_count = run_count;
g_queue_push_tail(queue, (gpointer) link);

and when I needed to use it in a different namespace I just当我需要在不同的命名空间中使用它时,我只是

struct wrapper* frame = (struct wrapper*) g_queue_pop_head(queue);
y4m_write_frame (fd_out, &ostreaminfo, &oframeinfo, frame->reconstructed);
// rest of the code

Thank you so much for your views and replies非常感谢您的意见和回复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM