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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:22:33+00:00 2026-06-03T15:22:33+00:00

Modified the below circular queue code for my app. This queue can hold 32

  • 0

Modified the below circular queue code for my app.

This queue can hold 32 elements max and I have declared the elements as a structure array inside the class. For adding an element to the queue you have to call CreateElement() functions, which checks for a free element and returns an index. When I reuse an element after processing the following line in the CreateElement functions crashes

    boost::shared_array<char> tData(new char[bufferSize]);

    m_QueueStructure[queueElems].data = tData;

As per documentation, the assignment operator is supposed to destroy the earlier object and assign the new one. Why is it crashing? Can someone tell me where am I screwing?

#include "boost/thread/condition.hpp"
#include "boost/smart_ptr/shared_array.hpp"
#include <queue>

#define MAX_QUEUE_ELEMENTS 32

typedef struct queue_elem
{
    bool            inUse;
    int             index;
    int             packetType;
    unsigned long   compressedLength;
    unsigned long   uncompressedLength;
    boost::shared_array<char> data;
}Data;

class CQueue
{
private:
    int                         m_CurrentElementsOfQueue;
    std::queue<Data>            the_queue;
    mutable boost::mutex        the_mutex;
    boost::condition_variable   the_condition_variable;
    Data                        m_QueueStructure[MAX_QUEUE_ELEMENTS];

public:

    CQueue()
    {
        m_CurrentElementsOfQueue = 0;

        for(int i = 0; i < MAX_QUEUE_ELEMENTS; i++)
        {
            m_QueueStructure[i].inUse = false;
            m_QueueStructure[i].index = i;
        }
    }

    ~CQueue()
    {
        for(int i = 0; i < m_CurrentElementsOfQueue; i++)
        {
            int index = wait_and_pop();

            Data& popped_value = m_QueueStructure[index];

            popped_value.inUse = false;
        }
        m_CurrentElementsOfQueue = 0;
    }

    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;
    }

    int wait_and_pop()
    {
        boost::mutex::scoped_lock lock(the_mutex);

        while(the_queue.empty())
        {
            the_condition_variable.wait(lock);
        }

        Data& popped_value=the_queue.front();

        the_queue.pop();

        return popped_value.index;
    }

    int CreateElement(int bufferSize, unsigned long _compressedLength, 
        unsigned long _uncompressedLength, int _packetType) /* Send data length for this function */
    {

        int queueElems = 0;

        if(m_CurrentElementsOfQueue == 32)
        {
            CCommonException ex(QERROR, QUEUE_FULL, "Circular Buffer Queue is full");
            throw ex;
        }

        for(queueElems = 0; queueElems < MAX_QUEUE_ELEMENTS; queueElems++)
        {
            if(m_QueueStructure[queueElems].inUse == false)
                break;
        }

        boost::shared_array<char> tData(new char[bufferSize]);

        m_QueueStructure[queueElems].data = tData;

        m_QueueStructure[queueElems].inUse  = true;

        m_QueueStructure[queueElems].compressedLength = _compressedLength;

        m_QueueStructure[queueElems].uncompressedLength = _uncompressedLength;

        m_QueueStructure[queueElems].packetType         = _packetType;

        m_CurrentElementsOfQueue++;

        return queueElems;
    }

    Data& GetElement(int index)
    {
        Data& DataElement = m_QueueStructure[index];

        return DataElement;
    }

    void ClearElementIndex(Data& delValue)
    {
        m_CurrentElementsOfQueue--;

        delValue.inUse = false;
    }

};
  • 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-06-03T15:22:36+00:00Added an answer on June 3, 2026 at 3:22 pm

    Solved the problem. Two changes I did. In the wait_and_pop function, I was returning an index rather than a Data&. When I returned Data&, that solved the assignment problem. Another crash was happening due to a memset of a shared_array.get(). Lesson learnt, never memset a shared_array or a shared_ptr.

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

Sidebar

Related Questions

In the code below I have changed show() to css() and modified the visibility.
I have this below code and i want to have a fadeIn effect to
I have tried using the below code modified from http://www.html5rocks.com/tutorials/file/dndfiles/ to read in a
I have this below code where i filter out type which are not of
Below class is a textbox field. Can this be modified so that when the
I have this code below and want it to show the progress of a
I have an app modified to take into account the UAC in VISTA. So,
i've been modified the code like below <script type=text/javascript> function addtext() { var barCode
SEE EDIT UPDATES BELOW. Original question has been modified! I have a working window
In the below code snippet can i replace char * to const char *

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.