2011-11-26 35 views
0

我有一個輸出函數,它將一些參數寫入帶有循環的文件。我在函數崩潰之前多次調用函數,並且它的功能正常。循環返回「count」參數中指定的次數。當計數爲6000時,它在5960處崩潰。該函數在先前的調用中不會崩潰,這會將較小的計數傳遞給該函數。如何解決在C++中的flsgetvalue()堆腐敗?

錯誤是指我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; 
} 

初始化從主要是:

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」。奇怪的是,它不會給其他實例帶來錯誤。

+0

你能更詳細地描述你的錯誤是什麼嗎?也許發佈整個代碼呢? (Somewhere?) –

+0

@C約翰遜我試圖在最後增加一些細節。謝謝。 –

回答

1

對不起,但沒有可重複的代碼我無法幫助你。我沒有調用堆棧,我沒有數據發送到你的函數中,我沒有可以編譯和運行的工作代碼。這裏使用的標準庫非常防彈。這意味着問題很可能是你自己的。

要調試這一點,我會做到這一點:

  1. 註釋掉的代碼,減少代碼回基本上沒什麼。
  2. 通過執行一個基本的shell來讓代碼運行到最後。確保它沒有錯誤地運行。
  3. 在visual studio中打開調試菜單選項中的所有'異常中斷'。這也被稱爲第一次機會例外的休息。
  4. 取消註釋您的代碼一次一行並運行完成。確保它運行沒有錯誤。
  5. 重複第4步,直到取消註釋所有代碼。
  6. 確保您的調試配置與調試庫鏈接,並且您的發佈配置與發佈庫鏈接。
+0

@C約翰遜。謝謝,我明白,遠程提出解決這個問題的方法幾乎是不可能的。 –