2012-05-09 7 views
0

私のアプリケーションの以下の循環キューコードが変更されました。boost :: shared_array割り当てがクラッシュするアプリケーション(VC++ 2010)

このキューは最大32個の要素を保持でき、クラス内の構造体配列として要素を宣言しています。キューに要素を追加するには、空要素をチェックしてインデックスを返すCreateElement()関数を呼び出す必要があります。私はのcreateElement関数で次の行を処理した後に要素を再利用するときにドキュメントを1として

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

    m_QueueStructure[queueElems].data = tData; 

をクラッシュし、代入演算子は、以前のオブジェクトを破棄し、新しいものを割り当てることになっています。なぜそれはクラッシュですか?誰かが私がどこにいじっているのか教えてくれますか?あなたは33番目の要素にm_QueueStructure[queueElems].dataにアクセスしようとしているので、queueElemsをループした後

#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

問題を解決を使用してみてください。私が行った2つの変更。 wait_and_pop関数では、データ&ではなくインデックスを返していました。データ&を返すと、割り当て問題が解決されました。 shared_array.get()のmemsetのために別のクラッシュが発生しました。レッスンでは、shared_arrayまたはshared_ptrをmemsetしたことはありません。

0

for(queueElems = 0; queueElems < MAX_QUEUE_ELEMENTS; queueElems++)は、値32が、あなたのm_QueueStructureのみ32の要素にしています。その問題。

EDIT:m_QueueStructure[queueElems].data.reset(new char[bufferSize]);

+0

使用された要素が2で、第1のインデックスで要素を再利用しようとしているときに問題が発生します。キューに要素を追加する他の方法はありますか? –

+0

どのようなエラーがありますか –

+0

割り当てステートメントでクラッシュしました。 –

関連する問題