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 3406874
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:40:54+00:00 2026-05-18T05:40:54+00:00

So I have a boosted queue class that helps multi-threading described here . In

  • 0

So I have a boosted queue class that helps multi-threading described here.

In my class declarations I have

//...
    struct VideoSample
    {   
        const unsigned char * buffer;
        int len;
    };

    ConcurrentQueue<VideoSample * > VideoSamples;

//...

struct AudioSample
{   
    const unsigned char * buffer;
    int len;
};

ConcurrentQueue<AudioSample * > AudioSamples;

//...

In my class I have a function:

void VideoEncoder::AddFrameToQueue(const unsigned char *buf, int size )
{
    VideoSample * newVideoSample = new VideoSample;
    VideoSamples.try_pop(newVideoSample);

    newVideoSample->buffer = buf;
    newVideoSample->len = size;
    VideoSamples.push(newVideoSample);
    //free(newVideoSample->buffer);
    //delete newVideoSample;
}

keeping only one frame in queue is required for my app.

answer provided here on how to delete a structure is not helpful in this case because app crushes.

I have similar code for audio queue.

void VideoEncoder::AddSampleToQueue(const unsigned char *buf, int size )
{
    AudioSample * newAudioSample = new AudioSample;
    newAudioSample->buffer = buf;
    newAudioSample->len = size;
    AudioSamples.push(newAudioSample);
    url_write (url_context, (unsigned char *)newAudioSample->buffer, newAudioSample->len);
    AudioSamples.wait_and_pop(newAudioSample);
    //delete newAudioSample;
}

and a function that runs in separate thread:

void VideoEncoder::UrlWriteData()
{
    while(1){
        switch (AudioSamples.empty()){
        case true : 
            switch(VideoSamples.empty()){
        case true : Sleep(5); break;
        case false :    
            VideoSample * newVideoSample = new VideoSample;
            VideoSamples.wait_and_pop(newVideoSample);
            url_write (url_context, (unsigned char *)newVideoSample->buffer, newVideoSample->len);
            break;
            } break;
        case false :  Sleep(3);     break;
        }
    }
}

BTW: to stream media data to url I use ffmpeg’s function.

BTW: here code I use for queues:

#include <string>
#include <queue>
#include <iostream>

// Boost
#include <boost/thread.hpp>
#include <boost/timer.hpp>

template<typename Data>
class ConcurrentQueue
{
private:
    std::queue<Data> the_queue;
    mutable boost::mutex the_mutex;
    boost::condition_variable the_condition_variable;
public:
    void push(Data const& data)
    {
        boost::mutex::scoped_lock lock(the_mutex);
        the_queue.push(data);
        lock.unlock();
        the_condition_variable.notify_one();
    }

    bool empty() const
    {
        boost::mutex::scoped_lock lock(the_mutex);
        return the_queue.empty();
    }

    bool try_pop(Data& popped_value)
    {
        boost::mutex::scoped_lock lock(the_mutex);
        if(the_queue.empty())
        {
            return false;
        }

        popped_value=the_queue.front();
        the_queue.pop();
        return true;
    }

    void wait_and_pop(Data& popped_value)
    {
        boost::mutex::scoped_lock lock(the_mutex);
        while(the_queue.empty())
        {
            the_condition_variable.wait(lock);
        }

        popped_value=the_queue.front();
        the_queue.pop();
    }

    Data& front()
    {
        boost::mutex::scoped_lock lock(the_mutex);
        return the_queue.front();
    }

};

My question is: How to clean up AddSampleToQueue and AddFrameToQueue so that they would not make memory leaks?

BTW: I am quite new to all this C++ shared/auto stuff. So to say a beginner. So please provide code examples that work at least that are integrated into my code – because I provided all my code. So if you know what to do – please try and integrate your knowledge into my example. Thank you. And if you can show me how to do it with no use of shared/auto ptrs I’ d be super glad.

  • 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-18T05:40:54+00:00Added an answer on May 18, 2026 at 5:40 am

    change your function to the below and do similar changes in other place where you are allocating memory.

    void VideoEncoder::AddFrameToQueue(const unsigned char *buf, int size )
    {

    VideoSample * newVideoSample;
    if(!VideoSamples.try_pop(newVideoSample))
    {
        newVideoSample  = new VideoSample; 
    }
    else
    {
       delete buff;
    }
    
    newVideoSample->buffer = buf; 
    newVideoSample->len = size; 
    VideoSamples.push(newVideoSample); 
    

    }

    ANd also I cannot stop myself asking this question.. When you want only one item in queue then why do you need queue at all.

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

Sidebar

Related Questions

I have to implement MPI system in a cluster. If anyone here has any
I have a function foo that takes a NxM numpy array as an argument
I have some code that does a lot of comparisons of 64-bit integers, however
Have noticed issue while testing iphone app that if one quickly opens/dismisses a modal
Have following setup: MainActivity class - extends activity MyLayout class - extends View Prefs
I have a project that adds elements to an AutoCad drawing. I noticed that
I have a script that appends some rows to a table. One of the
I have a new web app that is packaged as a WAR as part
To index my website, I have a Ruby script that in turn generates a
Have just started using Google Chrome , and noticed in parts of our site,

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.