1、勾選project-->Options-->debugging-->other debugging info
注意:一定要勾上Generate info for the debugger(slower/increases exe-size),否則展開后看不到行號(hào)信息。
注意:一定要勾上Generate info for the debugger(slower/increases exe-size),否則展開后看不到行號(hào)信息。

在lpr文件添加:
SetHeapTraceOutput('heaptrc.trc')
如果不保存為文件,將在屏幕顯示相應(yīng)的內(nèi)存泄漏信息。
program Project1; {$mode objfpc}{$H+} uses {$IFDEF UNIX} cthreads, {$ENDIF} {$IFDEF HASAMIGA} athreads, {$ENDIF} Interfaces, // this includes the LCL widgetset Forms, Unit1, LanZouAPI { you can add units after this }; {$R *.res} begin SetHeapTraceOutput('heaptrc.trc');//將Heapttrc的輸出保存為日志文件 RequireDerivedFormResource:=True; Application.Scaled:=True; {$PUSH}{$WARN 5044 OFF} Application.MainFormOnTaskbar:=True; {$POP} Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end.
2、利用日志文件查看具體出錯(cuò)位置對(duì)應(yīng)的代碼行
1)打開View-->Leaks and Traces

2)打開日志文件:

點(diǎn)"Resolve"會(huì)提示打開相應(yīng)的文件(如果你的調(diào)試信息包含在可執(zhí)行文件,就選可執(zhí)行文件。如果是外部調(diào)試信息 就選外部調(diào)試信息的gdb文件)

展開相應(yīng)的call Trace 列表然后雙擊相應(yīng)對(duì)行就可以在lazarus IDE打開對(duì)應(yīng)文件位置,這就是存在內(nèi)存泄漏的代碼。

======================================================================================
以下是阿D處理并顯示出錯(cuò)信息的方法:
//顯示錯(cuò)誤信息 procedure DumpExceptionCallStack(E: Exception); var I: integer; Frames: PPointer; Report: string; begin Report := 'Program exception! ' + LineEnding + 'Stacktrace:' + LineEnding + LineEnding; if E <> nil then begin Report := Report + 'Exception class: ' + E.ClassName + LineEnding + 'Message: ' + E.Message + LineEnding; end; Report := Report + BackTraceStrFunc(ExceptAddr); Frames := ExceptFrames; for I := 0 to ExceptFrameCount - 1 do Report := Report + LineEnding + BackTraceStrFunc(Frames[I]); ShowMessage(Report); Halt; // End of program execution end;
procedure TForm1.Button4Click(Sender: TObject); begin try raise Exception.Create('Test error'); except on E: Exception do DumpExceptionCallStack(E); end; end;
ExceptFrameCount //異常回溯中包含的幀數(shù) ExceptFrames //返回當(dāng)前異常堆棧幀 ExceptAddr //當(dāng)前異常地址. ExceptObject //當(dāng)前異常對(duì)象. ExceptProc //當(dāng)前異常處理程序. get_caller_addr(get_frame); // 得到調(diào)用者的地址 get_caller_frame(get_frame); // 得到調(diào)用者的棧幀

以上方法是從谷草及阿D那學(xué)到的,謝謝2位的無私分享!

浙公網(wǎng)安備 33010602011771號(hào)