我有一個輸出函數,它將一些參數寫入帶有循環的文件。我在函數崩潰之前多次調用函數,並且它的功能正常。循環返回「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」。奇怪的是,它不會給其他實例帶來錯誤。
你能更詳細地描述你的錯誤是什麼嗎?也許發佈整個代碼呢? (Somewhere?) –
@C約翰遜我試圖在最後增加一些細節。謝謝。 –