關於函式結束時 create的物件Free的問題 |
答題得分者是:kadee
|
polom
一般會員 發表:15 回覆:14 積分:15 註冊:2010-08-05 發送簡訊給我 |
|
老大仔
尊榮會員 發表:78 回覆:837 積分:1088 註冊:2006-07-06 發送簡訊給我 |
|
kadee
高階會員 發表:11 回覆:141 積分:165 註冊:2002-03-20 發送簡訊給我 |
|
polom
一般會員 發表:15 回覆:14 積分:15 註冊:2010-08-05 發送簡訊給我 |
|
老大仔
尊榮會員 發表:78 回覆:837 積分:1088 註冊:2006-07-06 發送簡訊給我 |
|
kadee
高階會員 發表:11 回覆:141 積分:165 註冊:2002-03-20 發送簡訊給我 |
try ... 不管中間有無錯誤 finally 都會執行 XXX.free; end 請參考下列的說明, he Finally keyword is used to mark the start of the final block of statements in a Try statement. They are executed regardless of what happens in the Try statements. However, the Finally clause does not actually handle any exceptions - the program will terminate if no Except clause is found (see notes below). Try-Finally is normally used by a routine to allow cleanup processing to take place, such as freeing resources, with the exception being correctly passed to the caller to handle. ===================引 用 polom 文 章=================== 請問各位前輩: [code delphi] Var stream: TMemerystream ; begin stream.create ; //做了些事情 // stream.free ; end ; [/code] 如果在stream.free「之前」出錯跳出函式,那stream會因為函式結束而Free嗎? 還是會成為空懸的記憶體?
------
Kadee/BigRed Ent. www.tw165.com |
polom
一般會員 發表:15 回覆:14 積分:15 註冊:2010-08-05 發送簡訊給我 |
|
kadee
高階會員 發表:11 回覆:141 積分:165 註冊:2002-03-20 發送簡訊給我 |
如果程式在執行到 stream.free 之前發生錯誤跳掉了,
當然 stream就無法被釋放。所以才要用 try ... finally stream.free end; 包起來,確保物件create 後,會釋放完全。 不過話說回來,現在PC記憶體動不動就 2G甚至到 4G。 要搞到[記憶體不足]還真不容易。 真的發生了就請user重開機。 :) ===================引 用 polom 文 章=================== 感謝各位前輩的指導 我原本的寫作習慣是 procedure XXX begin stream:=XX.create ; //中間不會使用try stream.free ; end ; 有同事跟我說如果沒有用Try來包的話,當函式中間出錯時Stream「可能」不會完整的釋放。 這個「可能」性存在嗎?
------
Kadee/BigRed Ent. www.tw165.com |
sryang
尊榮會員 發表:39 回覆:762 積分:920 註冊:2002-06-27 發送簡訊給我 |
|
老大仔
尊榮會員 發表:78 回覆:837 積分:1088 註冊:2006-07-06 發送簡訊給我 |
喔喔~~小弟受教了
小弟一直以為用 try ... except on E:Exce... do end; 才有辦法包住錯誤XD ===================引 用 kadee 文 章=================== try?? ...? 不管中間有無錯誤 finally??? 都會執行 ??? XXX.free; end 請參考下列的說明, he Finally keyword is used to mark the start of the final block of statements in a Try statement. They are executed regardless of what happens in the Try statements. ? However, the Finally clause does not actually handle any exceptions - the program will terminate if no Except clause is found (see notes below). ? Try-Finally is normally used by a routine to allow cleanup processing to take place, such as freeing resources, with the exception being correctly passed to the caller to handle. ===================引 用 polom 文 章=================== 請問各位前輩: [code delphi] Var stream: TMemerystream ; begin stream.create ; //做了些事情 // stream.free ; end ; [/code] 如果在stream.free「之前」出錯跳出函式,那stream會因為函式結束而Free嗎? 還是會成為空懸的記憶體? |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |