Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7674305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:42:32+00:00 2026-05-31T16:42:32+00:00

I’ve been grinding at trying to get a circular buffer working for days, and

  • 0

I’ve been grinding at trying to get a circular buffer working for days, and it’s just not playing ball for some reason. I’m writing the program in C, not C++.

I’m trying to buffer images from a camera in one thread, so it can be read and processed in another thread (I know about how to use pthread_mutex_* for resource locking). So far, I’ve managed buffer images onto my buffer but I’m having issues retrieving them. This is my circular buffer implementation:

#define BUFFER_SIZE 100

typedef struct
{
     IplImage** queue[BUFFER_SIZE];
     IplImage** in;
     IplImage** out;
     int num_frames;
     int in_ctr;
     int out_ctr;
     int update_flag;
 } frame_buffer;

static frame_buffer frbuff;

void buff_init()
{
    frbuff.in = &frbuff.queue[0];
    frbuff.out = &frbuff.queue[0];
    frbuff.num_frames = 0;
    frbuff.in_ctr = 0;
    frbuff.out_ctr = 0;
    frbuff.update_flag = 0;
}

int buff_size()
{
    return frbuff.num_frames;
}

int buff_flag_check()
{
    return frbuff.update_flag;
}

void buff_flag_set()
{
    frbuff.update_flag = 1;
}

void buff_flag_clr()
{
    frbuff.update_flag = 0;
}

IplImage** buff_get()
{
    IplImage** nextfr;
    if(frbuff.num_frames == 0)
    {
        return NULL;
    }
    nextfr = frbuff.out++;
    if(++frbuff.out_ctr == BUFFER_SIZE)
    {
        frbuff.out = &frbuff.queue[0];
        frbuff.out_ctr = 0;
    }
    --frbuff.num_frames;
    buff_flag_clr();
    return nextfr;
}

int buff_put(IplImage* nextfr)
{
    if(++frbuff.num_frames > BUFFER_SIZE)
    {
       return 0;
    }
    frbuff.in++; 
    frbuff.in = nextfr;
    if(++frbuff.in_ctr == BUFFER_SIZE)
    {
        frbuff.in = &frbuff.queue[0];
        frbuff.in_ctr = 0;
    }
    buff_flag_set();
    return 1;
}

I can buff_put() just fine, this appears to work as I’m seeing the counter increase. When I buff_get(), I also get something that’s not null. The issue comes when I try to do anything with it:

IplImage **some_image;
some_image = buff_get();
cvShowImage("some_window_somewhere",some_image);

Well apparently it’s unhappy with that…

OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /home/fagg/src/OpenCV-2.3.1/modules/core/src/array.cpp, line 2482
 terminate called after throwing an instance of 'cv::Exception'
 what():  /home/fagg/src/OpenCV-2.3.1/modules/core/src/array.cpp:2482: error: (-206)  Unrecognized or unsupported array type in function cvGetMat

I also tried this (which results in a segmentation fault):

cvShowImage("some_window_somewhere",*some_image);

I’m very stuck, have tried everything I can think of. No doubt it will end up being something obvious (I hate pointers). Any help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:42:33+00:00Added an answer on May 31, 2026 at 4:42 pm

    I am going to make an assumption here and assuming you are trying to build a queue of ImpImage*’s. If that is the case, this line:

    IplImage** queue[BUFFER_SIZE];
    

    should read:

    IplImage* queue[BUFFER_SIZE];
    

    And these lines:

    IplImage** buff_get()
    {
        IplImage** nextfr;
        if(frbuff.num_frames == 0)
        {
            return NULL;
        }
        nextfr = frbuff.out++;
    

    should read:

    IplImage* buff_get()
    {
        IplImage* nextfr;
        if(frbuff.num_frames == 0)
        {
            return NULL;
        }
        nextfr = *frbuff.out++;
    

    And these lines:

    IplImage **some_image;
    some_image = buff_get();
    cvShowImage("some_window_somewhere",some_image);
    

    should read:

    IplImage *some_image;
    some_image = buff_get();
    cvShowImage("some_window_somewhere",some_image);
    

    Next we have a straightforward bug. This line:

    frbuff.in = nextfr;
    

    should read:

    *frbuff.in = nextfr;
    

    I am surprised a modern C compiler didn’t reject the code outright. At the very least it must be giving a warnings. If you are using gcc I suggest passing -Wall -Wextra as a minimum for all new code you write. Getting your code into a form the compiler accepts without warnings may be painful, but in the long run as you are discovering not doing that is even more painful.

    Secondly, you say you are using pthreads. Most of your code will be OK if buf_get() is always called from the same thread, and likewise for buf_put(). If buf_get() is called from more than one thread then at the very least your queue will be corrupted, and ditto for buf_put(). The one exception to all this is frbuff.update_flag which is altered from both buf_get() and buf_put(). If buf_get() and buf_put() are called from different threads you can be assured update_flag will end up containing the wrong thing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.