背景:我正在研究一些代碼來讀取文件中的數據。數據示例由換行符分隔。此外,還有一個元數據級別的數據和一個分號作爲分隔符來表示序列已到達結束。該文件包含許多序列。我想打開文件,讀取一行數據並將其作爲矢量存儲,使用數據做一些事情,然後在下一行讀取......直到文件結束。這是內存泄漏嗎?內核資源泄露? (C++,並行工作室)
下面的代碼編譯得很好,當在我的linux機器上使用valgrind運行時,沒有發現內存泄漏。但是,當我在實驗室的Windows機器上的Parallel Studio中使用C++檢查器工具時,它會在我的程序中報告與內存相關的錯誤,無論是在此文件中。
內存泄漏的報道,似乎從線到幹:
ss>>number;
,也是一個內核資源泄漏的報道如下:
data.open(filename.c_str());
誰能幫助我瞭解爲什麼我收到這些錯誤,我應該怎麼做才能糾正它們?我沒有看到爲什麼這是一個內存泄漏,我更不確定內核資源錯誤。另外,如果我在這裏做任何愚蠢的事情,請隨時讓我知道!
class Network;
namespace data {
static std::string trainfiles[] = {
"/home/samurain/htm_check/data_files/train/sequence1.train",
"/home/samurain/htm_check/data_files/train/sequence2.train"};
static std::string testfiles[] = {
"/home/samurain/htm_check/data_files/train/sequence1.train",
"/home/samurain/htm_check/data_files/train/sequence2.train"};
}
const char SEQ_DELIM = ';';
struct Example
{
std::vector<int> stimulus;
std::fstream data;
bool clear() {stimulus.clear();}
bool open_file(std::string & filename)
{
data.open(filename.c_str());
if (!data)
{
std::cout <<"error opening file\n";
return false;
}
std::cout<<"opening file... " <<filename <<" opened \n";
return true;
}
bool close_file()
{
std::cout<<"closing file... ";
data.close(); data.clear();
std::cout<<"closed\n";
return true;
}
bool read_next(Network * myNetwork, bool & new_seq)
{
if (!data)
{
std::cout<<"file not opened" <<std::endl;
return false;
}
if (data.peek() == SEQ_DELIM)
{
data.ignore(100,'\n');
myNetwork->clearStates();
new_seq = true;
}
int number = 300; //assuming input will be integer values, not set to 0 for debugging purposes
std::string line;
getline(data, line);
if (!data.good())
{
std::cout<<"end of file reached" <<std::endl;
return false;
}
std::stringstream ss(line);
while (ss)
{
ss>>number;
if (ss.good())
{
stimulus.push_back(number);
}
}
return true;
}//end read next
};//end of Example
感謝您的提示。有關Valgrind和PS的更多信息! – ryguy 2011-04-23 23:15:54