2012-05-09 47 views
0

修改了我的應用程序的以下循環隊列代碼。boost :: shared_array賦值崩潰應用程序(VC++ 2010)

這個隊列最多可以容納32個元素,並且我已經將這些元素聲明爲類內的結構數組。爲了向隊列添加一個元素,你必須調用CreateElement()函數,它檢查一個空閒元素並返回一個索引。當我重用處理下面的行中的createElement功能之後的元素崩潰

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

    m_QueueStructure[queueElems].data = tData; 

作爲每文檔,賦值操作符被認爲破壞早期對象並分配新的。它爲什麼會崩潰?有人能告訴我我在哪裏擰?

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

}; 

回答

0

解決的問題。我做了兩件改變。在wait_and_pop函數中,我返回索引而不是數據&。當我返回數據&時,解決了分配問題。由於shared_array.get()的memset,導致另一次崩潰。學到的教訓,永遠不會memset一個shared_array或shared_ptr。

0

for(queueElems = 0; queueElems < MAX_QUEUE_ELEMENTS; queueElems++)循環queueElems後有值32,但在你的m_QueueStructure只有32元,所以你試圖訪問m_QueueStructure[queueElems].data至33元。這個問題。

編輯:嘗試使用m_QueueStructure[queueElems].data.reset(new char[bufferSize]);

+0

問題發生在使用元素爲2時,我試圖在第一個索引處重用該元素。任何其他更好的方式添加元素排列? –

+0

你有什麼樣的錯誤 –

+0

這是在賦值語句中的崩潰。 –