我有一个 c++ 代码,我使用 MSC9 来编译它。它一直在随机崩溃。例如,如果使用 `` 从 Perl 调用它会崩溃,但从命令行或 Ultimate++ 调用它不会崩溃。
我的意思是从 perl 中调用它,例如。f.exe arg1 arg2 arg3
堆栈跟踪没有显示太多。逐行跟踪程序证明程序在返回时最后失败...
所以就是这样
int funcname()
{
return 0; <-- crashing after that...
}
我猜堆栈已损坏,堆栈展开后,它崩溃了..
什么会导致它?该程序使用 pcre、stl 和迭代器。迭代器可以破坏堆栈吗?你会如何捕捉到这样的错误?
它可能是编译器错误吗?
注意:调试版不会崩溃,只有发布版...
该错误似乎与此 pvector 类有关。
我有一个类似于这样的结构:
struct complexstr
{
pvector<int> v;
string v2;
hash_map<string> hm;
vector<string> vs; // similar
int i;
};
它似乎失败了,因为这一行:
complexstr s1;
complexstr s2;
s2=s1; // it seems to fail here, if this is not there... there is no error.
我认为问题出在下面的类... std::copy 在 pvector operator=(const pvector &pv) 中是正确的,对吗?
pvector 是一个 perl 兼容的向量...它的索引可以大于向量的分配大小。
更新 1:我收到了作业中存在泄漏的建议。我改变了分配......现在看起来是这样的:
pvector& operator=(const pvector &pv)
{
delete [] m_rgArray;
m_rgArray=new value_type[pv.allocated];
m_nIndex=pv.m_nIndex;
allocated=pv.allocated;
std::copy(pv.m_rgArray, pv.m_rgArray + pv.allocated, m_rgArray);
return *this;
}
注意:通过将 & 添加到返回类型,崩溃仍然存在。但是,删除泄漏后,添加 delete [] m_rgArray; ,程序不再崩溃。我不明白。据我所知,泄漏不会导致崩溃。所以问题似乎解决了(?)。问号表示我的惊讶。Update2:不,问题又回来了。它只是消失了一段时间。Update3:我想我已经找到了。我使用了来自 Microsoft 调试工具的实用程序 gflags.exe 和 windbg.exe 来查找确切位置。我使用 gflags.exe /p /enable myprog.exe /full 来打开堆错误的异常。目前,我认为该错误是由 FindClose(handle); 引起的。其中句柄是一个随机值,未初始化。
旧版:
template<class _Ty>
class pvector
{
public:
_Ty * m_rgArray; // Declare array
int m_nIndex; // Index to array
int allocated;
_Ty undefvalue;
typedef _Ty value_type;
typedef value_type & reference;
typedef const value_type & const_reference;
typedef custom_iterator<_Ty> iterator;
typedef custom_iterator<_Ty> const_iterator;
typedef int difference_type;
typedef int size_type;
//typedef typename pvector_type_traits<_Ty>::default_value default_value;
pvector() : m_nIndex(0)
{ // init index to 0
m_rgArray = new value_type[10];
allocated = 10;
fill(0);
}
pvector(size_type s) : m_nIndex(0)
{ // init index to 0
size_type defsize = 10;
if (s>10)
{
defsize = s;
}
m_rgArray = new value_type[defsize];
allocated = defsize;
fill(0);
}
pvector(pvector const& pv)
: m_rgArray(new value_type[pv.allocated]),
m_nIndex(pv.m_nIndex),allocated(pv.allocated)
{
std::copy(pv.m_rgArray, pv.m_rgArray + pv.allocated, m_rgArray);
}
pvector operator=(const pvector &pv)
{
m_rgArray=new value_type[pv.allocated];
m_nIndex=pv.m_nIndex;
allocated=pv.allocated;
std::copy(pv.m_rgArray, pv.m_rgArray + pv.allocated, m_rgArray);
return *this;
}
void clear()
{
m_nIndex=0;
fill(allocated);
}
~pvector() {
delete []m_rgArray;
}
size_type size() const
{ // return length of sequence
return m_nIndex;
}
size_type max_size() const
{ // return maximum possible length of sequence
return 0;
}
void fill(size_type si)
{
for (size_type i = si;i<allocated;i ++ )
{
m_rgArray[i] = pvector_type_traits<_Ty>::default_value();
}
}
bool empty() const
{ // test if sequence is empty
return (m_nIndex > 0 ? false : true);
}
iterator begin()
{ // return iterator for beginning of mutable sequence
return iterator(&m_rgArray[0]);
}
const_iterator begin() const
{
return const_iterator(&m_rgArray[0]);
}
iterator end()
{ // return iterator for end of mutable sequence
return iterator(&m_rgArray[m_nIndex]);
}
const_iterator end() const
{
return const_iterator(&m_rgArray[m_nIndex]);
}
reference operator[](size_type i)
{
if (m_nIndex>i)
{
return m_rgArray[i];
}
else if (i >= allocated)
{
resize(i * 2);
}
m_nIndex = i + 1;
return m_rgArray[i];
}
void resize(size_type s)
{
value_type * m_rgArray2;
size_type old_allocated = allocated;
allocated = s;
m_rgArray2 = new value_type[allocated];
//if (allocated>m_nIndex)
//{
// m_nIndex=allocated;
// }
// cout <<"m_nIndex" << m_nIndex << "allocated" << allocated << endl;
if (m_nIndex>allocated)
{
m_nIndex=allocated;
}
for (size_type i = 0;i<m_nIndex;i ++ )
{
m_rgArray2[i] = m_rgArray[i];
}
delete []m_rgArray;
m_rgArray = m_rgArray2;
fill(old_allocated);
}
reference back()
{
return &m_rgArray[m_nIndex - 1];
}
const_reference back() const
{
return m_rgArray[m_nIndex - 1];
}
void push_back(const _Ty &_Val)
{ // insert element at end
if (size() < allocated)
m_rgArray[m_nIndex ++ ] = _Val;
else
{
resize(allocated * 2);
m_rgArray[m_nIndex ++ ] = _Val;
}
}
};