我有一个C++代码,我使用MSC9来编译它。 它随机崩溃。例如,如果它是使用``从Perl调用的,但它在从命令行或从Ultimate ++调用它时不会崩溃。程序在离开C++函数时崩溃....你认为它是什么?
我的意思是从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 ::复制在pvector运算符中是正确的=(const pvector & pv),对不对?
pvector是一个Perl兼容的向量...其索引可以大于向量的分配大小。
Update1: 我收到了有关作业泄漏的建议。 我改变了分配...... 这就是现在的样子:
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; ,该程序不再崩溃, 。我不明白。据我所知,泄漏 不会导致崩溃。所以这个问题似乎解决了(?)。问号显示我的惊讶。更新2: 不,问题回来了。它只是消失了一段时间。 Update3:我想我已经找到它了。我使用了一个名为gflags.exe和windbg.exe的调试工具的实用程序来查找确切位置。 我使用了gflags.exe/p/enable myprog.exe/full来打开堆错误的例外情况。 目前,我认为这个错误是由FindClose(句柄)引起的。句柄是一个随机值,没有初始化。
旧版本:
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;
}
}
};
Ofcourse这是一个编译器错误! :) 这将有助于,如果你张贴功能的一些代码。 – Indy9000 2009-07-31 09:18:55
它可能是任何东西。显示一些代码。捕捉这种错误的最快捷方式通常是非常仔细地通读代码。 – nos 2009-07-31 09:20:31
以下是一些代码 – Aftershock 2009-07-31 11:38:21