我得到了下面一段代碼,我已經縮小了內存泄漏的範圍(即,在任務管理器中,專用工作集的內存隨着相同的重複輸入而增加串)。我理解堆和棧內存的概念,以及爲避免內存泄漏的一般規則,但一些地方仍然會錯誤:在C++中識別內存泄漏
while(!quit){
char* thebuffer = new char[210];
//checked the function, it isn't creating the leak
int size = FuncToObtainInputTextFromApp(thebuffer); //stored in thebuffer
string bufferstring = thebuffer;
int startlog = bufferstring.find("$");
int endlog = bufferstring.find("&");
string str_text="";
str_text = bufferstring.substr(startlog,endlog-startlog+1);
String^ str_text_m = gcnew String(str_text_m.c_str());
//some work done
delete str_text_m;
delete [] thebuffer;
}
我能想到的唯一的事情是它可能是創造'string str_text',因爲它從來沒有超出範圍,因爲它只是在這段時間重新開放?如果是這樣,我將如何解決?在while循環之外定義它並不能解決它,因爲它也會保留在範圍之內。任何幫助將不勝感激。
這不是C++,也就是C++/CLI。而在那個奇怪的混合。 – GManNickG 2010-06-16 20:27:03
這實際上是否會崩潰,最終呢?當你使用垃圾收集時,它可能只是使用可用內存來避免gc掃描。 – 2010-06-16 20:30:06
我不知道它是什麼,但它不是'string str_text',它存在於堆棧中,因此您不必擔心刪除它。 – 2010-06-16 20:30:39