-6
我正在寫一個程序與USB LCD顯示器(風暴5100-0103)交談,迄今爲止一切工作正常,除了我有一個內存泄漏的地方。C++內存泄漏使用char *
mainLoop: while (true) {
// Get the line
std::getline(std::cin,stringIn);
if (stringIn.find("~") == string::npos || stringIn.empty())
{
continue;
}
// split string to get line data and line number
string data;
int lineNo;
// get the data portion
data = stringIn.substr(0,stringIn.find("~"));
// create a string for line number
string lineNoString = stringIn.substr(stringIn.find("~") + 1, stringIn.length());
// get line number
lineNo = atoi(lineNoString.c_str());
// Create a char array to pass to the displaya
char *cstr = new char[data.size() + 1];
std::copy(data.begin(), data.end(), cstr);
cstr[data.size()] = '\0';
// Write line to the display
retval = usbDisplayPtr->DrawString(0, lineNo, 1, cstr, USBDisplayApi::FONT6X16, 3000);
cout << retval + "\n";
delete[] cstr;
}
任何幫助將不勝感激。
顯示您的功夫 – Steve 2014-09-18 18:52:44
您是否嘗試過valgrind? – taocp 2014-09-18 18:52:49
好像應該刪除cstr;而不是刪除[]給我 – Steve 2014-09-18 18:54:31