【问题标题】:How to solve flsgetvalue() heap corruption in C++?如何解决 C++ 中的 flsgetvalue() 堆损坏?
【发布时间】:2011-11-26 11:14:25
【问题描述】:

我有一个输出函数,它通过循环将一些参数写入文件。在它崩溃之前我多次调用该函数并且它正常运行。循环返回“count”参数中指定的次数。当计数为 6000 时,它在 5960 处崩溃。该函数不会在之前的调用中崩溃,这会将较低的计数传递给函数。

错误将我引向 tidtable.c 并指出以下几点:

_CRTIMP PFLS_GETVALUE_FUNCTION __cdecl __set_flsgetvalue()
{
#ifdef _M_IX86
    PFLS_GETVALUE_FUNCTION flsGetValue = FLS_GETVALUE; // <---exactly Here
    if (!flsGetValue)
    {
        flsGetValue = DecodePointer(gpFlsGetValue);
        TlsSetValue(__getvalueindex, flsGetValue);
    }
    return flsGetValue;

我使用以下模板在函数中初始化唯一动态分配的参数 pntr:

template <typename T>
T *AllocateDynamicVector(int nRows){
    T *dynamicArray;

    dynamicArray = new T[nRows];
    for( int i = 0 ; i < nRows ; i++ ){
        dynamicArray[i]= 0;
    }
    return dynamicArray;
}

来自main的初始化是:

    int* pntrPlane     = AllocateDynamicVector<int>(2*numberOfCells);
Writer(myfile,ss,"123","0","0","1","0","0","0","1","0",2*numberOfCells,lineInfo,1,pntrPlane);

代码如下:

void Writer (ofstream &myfile, stringstream &ss, std::string type, std::string id1, std::string id2, std::string id3,
       std::string id4,std::string id5,std::string id6,std::string id7,std::string id8, int count, 
       int lineInfo[2], int lineNum, int* pntr){
           if(myfile.is_open()){
               for (int i=0; i < count; i++){
                   if (type == "123"){
                       cout<<"I'm here!"<<" "<<i<<endl;
                   }
                   ss.seekp(0);
                   ss<<setw(80)<<lineInfo[0]; ss.seekp(0);
                   ss<<setw(73)<<"1D"; ss.seekp(0);
                   ss<<setw(56)<<id1.c_str(); ss.seekp(0);
                   ss<<setw(48)<<id2.c_str(); ss.seekp(0);
                   ss<<setw(40)<<id3.c_str(); ss.seekp(0);
                   ss<<setw(32)<<id4.c_str(); ss.seekp(0);
                   ss<<setw(24)<<id5.c_str(); ss.seekp(0);
                   ss<<setw(16)<<lineInfo[1]; ss.seekp(0);
                   ss<<setw(8)<<type.c_str(); ss.seekp(0);
                   myfile<<ss.str()<<endl; ss.clear();
                   pntr[i] = lineInfo[0];
                   lineInfo[0]++;
                   ss.seekp(0);
                   ss<<setw(80)<<lineInfo[0]; ss.seekp(0);
                   ss<<setw(73)<<"0D"; ss.seekp(0);
                   ss<<setw(40)<<id6.c_str(); ss.seekp(0);
                   ss<<setw(32)<<lineNum; ss.seekp(0);
                   ss<<setw(24)<<id8.c_str(); ss.seekp(0);
                   ss<<setw(16)<<"0"; ss.seekp(0);
                   ss<<setw(8)<<type.c_str(); ss.seekp(0);
                   myfile<<ss.str()<<endl; ss.clear();
                   lineInfo[0]++;
                   lineInfo[1] = lineInfo[1] + lineNum;
               }
           }
}

我怎样才能避免这个问题??

注意:如果我在没有调试的情况下运行程序,那么错误就会消失。

编辑:问题的详细描述:我正在使用上述函数多次生成输出文件。我向函数发送参数,包括一个动态整数向量,并在每次迭代时将一个数字写入其中。在使用一组参数执行此函数期间,它给出了堆损坏错误“检测到严重错误 c0000374”。奇怪的是它不会在其他实例中出错。

【问题讨论】:

  • 你能更详细地描述你的错误是什么吗?也许也发布整个代码? (某处?)
  • @C Johnson 最后我尝试添加更多细节。谢谢。

标签: c++ visual-studio-2010 heap-corruption


【解决方案1】:

抱歉,没有可重现的代码,我无法帮助您。我没有调用堆栈,我没有数据可以发送到你的函数中,我没有可以编译和运行的工作代码。您在这里使用的标准库几乎是防弹的。这意味着问题很可能是您自己的问题。

要调试它,我会这样做:

  1. 通过注释掉代码将您的代码减少到基本上没有。
  2. 通过执行基本 shell 使代码运行到最后。确保它运行时没有错误。
  3. 在 Visual Studio 中,打开调试菜单选项中的所有“异常中断”。这也称为“首次机会例外”中断。
  4. 一次取消注释您的代码一行并运行它直到完成。确保它运行时没有错误。
  5. 重复第 4 步,直到取消所有代码的注释。
  6. 确保您的调试配置与调试库相关联,并且您的发布配置与发布库相关联。

【讨论】:

  • @C Johnson。谢谢,我知道远程建议解决此问题几乎是不可能的。
猜你喜欢
  • 2019-05-30
  • 2013-09-29
  • 2011-12-03
  • 2011-08-03
  • 1970-01-01
  • 2010-10-17
  • 2019-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多