Visual c++ boost::共享_数组分配使应用程序崩溃(VC++2010)
修改了我的应用程序的以下循环队列代码 这个队列最多可以容纳32个元素,我已经将这些元素声明为类中的结构数组。要向队列中添加元素,必须调用CreateElement函数,该函数检查空闲元素并返回索引。当我在处理CreateElement函数中的以下行后重用元素时,会崩溃Visual c++ boost::共享_数组分配使应用程序崩溃(VC++2010),visual-c++,queue,shared-ptr,Visual C++,Queue,Shared Ptr,修改了我的应用程序的以下循环队列代码 这个队列最多可以容纳32个元素,我已经将这些元素声明为类中的结构数组。要向队列中添加元素,必须调用CreateElement函数,该函数检查空闲元素并返回索引。当我在处理CreateElement函数中的以下行后重用元素时,会崩溃 boost::shared_array<char> tData(new char[bufferSize]); m_QueueStructure[queueElems].data = tData; 根
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;
}
};
forqueuelems=0;队列元素<最大队列元素;循环queueElems后,queueElems++的值为32,但在m_QueueStructure中只有32个元素,因此您试图访问m_QueueStructure[queueElems]。第33个元素的数据。这就是问题所在
编辑:尝试使用m_QueueStructure[queueElems].data.resetnew char[bufferSize] 解决了这个问题。我做了两个改变。在wait_和pop函数中,我返回的是索引而不是数据&。当我返回数据-,这就解决了分配问题。另一个崩溃是由于共享_array.get的memset造成的。吸取的教训是,永远不要将共享数组或共享ptr设为memset。当使用的元素为2时,问题就会出现,我试图在第一个索引处重用该元素。向队列中添加元素还有其他更好的方法吗?它是赋值语句的崩溃。它是赋值语句的崩溃。不能复制粘贴整个事情-中间有呼叫缺失,因为有一个词限制评论> Test.EXE!操作员删除[]void*p=0x01aa6fd0行21+0x9字节C+访问冲突?堆腐败?请提供更多信息,我不确定。错误位于删除[]。所以我猜是堆腐败。