Aft*_*ock 1 c++ compiler-construction stack-unwinding
我有一个c ++代码,我用MSC9来编译它.它会随机崩溃.例如,如果从Perl使用``调用它会崩溃,但是当它从命令行或从Ultimate ++调用时它不会崩溃.
我的意思是从perl调用它,例如. f.exe arg1 arg2 arg3
堆栈跟踪显示不多.逐行跟踪程序证明程序在返回时失败了......
所以就是这样
int funcname()
{
return 0; <-- crashing after that...
}
Run Code Online (Sandbox Code Playgroud)
我猜堆栈已损坏,堆栈解除后,它崩溃了..
什么可以导致它?该程序使用pcre,stl和迭代器.迭代器可以打破堆栈吗?你怎么会遇到这样的错误?
它可以是编译器错误吗?
注意:调试版本不会崩溃,只会发布版本...
这个错误似乎与这个pvector类有关.
我有一个类似于这样的结构:
struct complexstr
{
pvector<int> v;
string v2;
hash_map<string> hm;
vector<string> vs; // similar
int i;
};
Run Code Online (Sandbox Code Playgroud)
它似乎失败了,因为这一行:
complexstr s1;
complexstr s2;
s2=s1; // it seems to fail here, if this is not there... there is no error.
Run Code Online (Sandbox Code Playgroud)
我认为问题出在下面的类... std :: copy在pvector operator =(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;
}
Run Code Online (Sandbox Code Playgroud)
注意:通过向返回类型添加&,崩溃仍然存在.但是,删除泄漏后,添加delete [] m_rgArray; ,程序不再崩溃.我不明白.据我所知,泄漏不会导致崩溃.所以这个问题似乎已经解决了(?).问号显示我的惊讶.Update2:不,问题又来了.它刚刚消失了一段时间.Update3:我想我已经找到了.我使用Microsoft调试工具中的一个名为gflags.exe和windbg.exe的实用程序来查找确切位置.我使用gflags.exe/p /启用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;
}
}
};
Run Code Online (Sandbox Code Playgroud)
sha*_*oth 12
它可能是一个缓冲区溢出来破坏堆栈.如果在运行函数时在本地定义的缓冲区外写入,它可以覆盖返回地址,然后从函数返回将触发程序崩溃.
您应该查找使用本地(堆栈分配)变量的地址操作的语句 - 它们上的缓冲区溢出很可能是问题的原因.
| 归档时间: |
|
| 查看次数: |
4107 次 |
| 最近记录: |