[英]Is there a memory leak in the following c++ code?
我正在為我的DataMining項目編寫程序。 我的代碼出現分段錯誤。 我嘗試使用gdb進行調試,並在ubuntu中使用ulimit創建了核心轉儲。
以下是我能夠在gdb中回溯的錯誤。
Python Exception <class 'gdb.MemoryError'> Cannot access memory at address 0x7fff48c47738: #0 0x000000000040cdb0 in TokenReader::getToken (
this=<error reading variable: Cannot access memory at address 0x7fff48c47718>,
this@entry=<error reading variable: Cannot access memory at address 0x7fff48c47738>,
buf=<error reading variable: Cannot access memory at address 0x7fff48c47710>,
buf@entry=<error reading variable: Cannot access memory at address 0x7fff48c47738>) at src/tokread.c++:26
Cannot access memory at address 0x7fff48c47738
我的代碼:
/*********************************************************
* Project: 2
* File: tokread.h
* Anil Pediredla, KUID 28056
*********************************************************/
#ifndef _TOKREAD_H_
#define _TOKREAD_H_
#include <iostream>
using namespace std;
const short NEW_LINE = 0;
const short SPACE = 1;
const short TOKEN = 2;
const short COMMENT = 3;
class TokenReader {
public:
TokenReader(istream &in);
char* getToken(char* buf);
private:
istream ∈
short state;
};
#endif //_TOKREAD_H_
/*********************************************************
* Project: 2
* File: tokread.h
* Anil Pediredla, KUID 28056
*********************************************************/
#include <sstream>
#include <cstdlib>
using namespace std;
#include "tokread.h"
/*********************************************************
* Class: TokenReader
*********************************************************/
TokenReader::TokenReader(istream &in):
in(in), state(NEW_LINE) {
}
char* TokenReader::getToken(char* buf) {
bool done = false;
int tokLen = 0;
while (!done && !in.eof()) { //error here
char ch = in.get();
switch (state) {
case NEW_LINE:
if (ch == '!') state = COMMENT;
else {
in.unget();
state = SPACE;
}
break;
case SPACE:
if ((ch == 13) && (in.peek() == '\n')) in.get();
if (ch == '\n') {
state = NEW_LINE;
break;
}
if ((ch != ' ') && (ch != '\t')) {
in.unget();
state = TOKEN;
}
break;
case TOKEN:
if ((ch == 13) && (in.peek() == '\n')) in.get();
if (ch == '\n') {
state = NEW_LINE;
done = true;
break;
}
if ((ch == ' ') || (ch == '\t')) {
in.unget();
state = SPACE;
done = true;
break;
}
buf[tokLen++] = ch;
break;
case COMMENT:
if (ch == '\n'){
state = NEW_LINE;
}
break;
}
}
buf[tokLen] = '\0';
return buf;
}
我通過一個看起來像這樣的主函數來調用我的類
int main(int argc, char* argv[]) { fstream input(inFileName, ios::in); if (!input) { cerr << "Could not open file " << inFileName << endl; exit(-1); /* calling my error class here. */ TokenReader reader(input); Table* table = new Table(reader, maxDistAttrVal); table->mlem2(intvFormat, output1, output2, output3); }
}
如果要為Window構建它,則可以使用它們為Visual Studioes提供的內置數據泄漏代碼(我尚未在Visual Basics之外測試過該代碼)
//-----MICROSOFT ONLY----------
#define _CRT_DEBUG_MAP_ALLOC
#include <crtdbg.h> //C-runtime debug header
//must be placed before include's
//------------------------------
//Must be place at the top of your main method
#if defined(_DEBUG)
int dbgFlags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
dbgFlags |= _CRTDBG_CHECK_ALWAYS_DF; //check block intergerity on every memory
dbgFlags |= _CRTDBG_DELAY_FREE_MEM_DF; //dont remove blocks on delete
dbgFlags |= _CRTDBG_LEAK_CHECK_DF; //check for leaks at process termination
_CrtSetDbgFlag(dbgFlags);
#endif
此代碼將確保清除所有備忘錄泄漏,並且不會刪除這些塊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.