Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp wanted on memory leak - having a multi-threading queue, char buffer and a structure
    text
    copied!<p>So I have a boosted queue class that helps multi-threading described <a href="http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html" rel="nofollow noreferrer">here</a>.</p> <p>In my class declarations I have</p> <pre><code>//... struct VideoSample { const unsigned char * buffer; int len; }; ConcurrentQueue&lt;VideoSample * &gt; VideoSamples; //... struct AudioSample { const unsigned char * buffer; int len; }; ConcurrentQueue&lt;AudioSample * &gt; AudioSamples; //... </code></pre> <p>In my class I have a function:</p> <pre><code>void VideoEncoder::AddFrameToQueue(const unsigned char *buf, int size ) { VideoSample * newVideoSample = new VideoSample; VideoSamples.try_pop(newVideoSample); newVideoSample-&gt;buffer = buf; newVideoSample-&gt;len = size; VideoSamples.push(newVideoSample); //free(newVideoSample-&gt;buffer); //delete newVideoSample; } </code></pre> <p>keeping only one frame in queue is required for my app.</p> <p>answer provided <a href="https://stackoverflow.com/questions/4134323/c-how-to-delete-a-structre/4134343#4134343">here</a> on how to delete a structure is not helpful in this case because app crushes.</p> <p>I have similar code for audio queue.</p> <pre><code>void VideoEncoder::AddSampleToQueue(const unsigned char *buf, int size ) { AudioSample * newAudioSample = new AudioSample; newAudioSample-&gt;buffer = buf; newAudioSample-&gt;len = size; AudioSamples.push(newAudioSample); url_write (url_context, (unsigned char *)newAudioSample-&gt;buffer, newAudioSample-&gt;len); AudioSamples.wait_and_pop(newAudioSample); //delete newAudioSample; } </code></pre> <p>and a function that runs in separate thread:</p> <pre><code>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-&gt;buffer, newVideoSample-&gt;len); break; } break; case false : Sleep(3); break; } } } </code></pre> <p>BTW: to stream media data to url I use ffmpeg's function.</p> <p>BTW: here code I use for queues:</p> <pre><code>#include &lt;string&gt; #include &lt;queue&gt; #include &lt;iostream&gt; // Boost #include &lt;boost/thread.hpp&gt; #include &lt;boost/timer.hpp&gt; template&lt;typename Data&gt; class ConcurrentQueue { private: std::queue&lt;Data&gt; the_queue; mutable boost::mutex the_mutex; boost::condition_variable the_condition_variable; public: void push(Data const&amp; 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&amp; 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&amp; 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&amp; front() { boost::mutex::scoped_lock lock(the_mutex); return the_queue.front(); } }; </code></pre> <p>My question is: How to clean up AddSampleToQueue and AddFrameToQueue so that they would not make memory leaks?</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload