最近程序总是出现各种非法访问错误,想找这种错误又比较麻烦,之前有看到网上介绍BCB自带的codeguard工具,据说挺好用的,就研究了一下,只是测试了个很简单的例子,感觉还挺好的,下面是测试过程:
创建一个程序:
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char *p = new char; //申请内存,没有释放
}
//---------------------------------------------------------------------------
开启codeguard调试:
右键工程Options
F9启动程序,结果弹出提示:Nonshared DATA segment required
网上找了好久都没有解决办法,最后在embarcadero论坛上找到一个解决办法,下面是网址
https://forums.embarcadero.com/thread.jspa?messageID=519396&tstart=0#519396
Options-C++Linker-Dynamic RTL 设置为false
Options-Packages-Runtime packages 去掉Build with runtime packages前面的“√”
重新编译后启动正常,关闭程序后,可以在exe目录下找到一个cgl后缀的文件,打开内容如下
Error 00176. 0x300010 (Thread 0x13DC):
Resource leak: The object (0x2483390) was never deleted
The object (0x02483390) [size: 1 bytes] was created with new
Call Tree:
0x00401E2C(=Project1.exe:0x01:000E2C) Unit1.cpp#19
0x00498161(=Project1.exe:0x01:097161) Controls.pas#7178
0x0049C4BD(=Project1.exe:0x01:09B4BD) Controls.pas#9819
0x004564C1(=Project1.exe:0x01:0554C1) StdCtrls.pas#4364
0x0049C610(=Project1.exe:0x01:09B610) Controls.pas#9888
0x0049C4BD(=Project1.exe:0x01:09B4BD) Controls.pas#9819
0x0047B94D(=Project1.exe:0x01:07A94D) Forms.pas#3960
0x0049BB5B(=Project1.exe:0x01:09AB5B) Controls.pas#9540
0x00421F8E(=Project1.exe:0x01:020F8E) Classes.pas#13015
0x7428D2B3(=USER32.dll:0x01:02C2B3)
0x7426E88A(=USER32.dll:0x01:00D88A)
0x7429764B(=USER32.dll:0x01:03664B)
0x74270C00(=USER32.dll:0x01:00FC00)
0x6BE4D36F(=COMCTL32.DLL:0x01:04C36F)
0x6BE5D065(=COMCTL32.DLL:0x01:05C065)
------------------------------------------
Functions called:
fflush (4 times)
delete (21 times)
memset (2 times)
SysReallocMem (68 times)
SysFreeMem (611 times)
SysGetMem (612 times)
free (20 times)
_lsetlocale (6 times)
new (31 times)
calloc (9 times)
strlen (131 times)
realloc (1 times)
strdup (5 times)
malloc (5 times)
memcpy (11 times)
Resource types used:
object (31 allocs, 31 max)
memory block (632 allocs, 312 max)
Modules used:
00400000 07/11/2018 16:14:58 C:\Users\yuwp\Desktop\111\Debug\Project1.exe
052B0000 02/24/2017 19:25:32 C:\Program Files
(x86)\SogouInput\8.2.0.9335\Resource.dll
0CD00000 04/05/2016 06:55:00 C:\Program Files
(x86)\Embarcadero\Studio\18.0\bin\CG32.DLL
10000000 03/02/2017 12:08:14 C:\Windows\system32\SogouPy.ime
50030000 04/05/2016 06:55:00 C:\Program Files
(x86)\Embarcadero\Studio\18.0\bin\borlndmm.DLL
646A0000 07/16/2016 19:42:46 C:\Windows\system32\apphelp.dll
6AC00000 01/10/2017 08:32:32 C:\Program Files
(x86)\SogouInput\Components\PicFace\1.0.0.1630\PicFace.dll
6AEE0000 10/10/2017 10:58:02 C:\Windows\SYSTEM32\OLEACC.dll
6B2D0000 10/10/2017 10:58:36 C:\Windows\SYSTEM32\iertutil.dll
6B720000 10/10/2017 10:58:36 C:\Windows\SYSTEM32\urlmon.dll
6BC80000 09/16/2016 01:13:52 C:\Windows\SYSTEM32\dwmapi.dll
6BE00000 10/10/2017 10:58:22
C:\Windows\WinSxS\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.
14393.953_none_89c2555adb023171\COMCTL32.DLL
6C230000 07/16/2016 19:42:50 C:\Windows\SYSTEM32\MSACM32.dll
6C480000 07/16/2016 19:43:06 C:\Windows\SYSTEM32\UxTheme.dll
6F720000 10/10/2017 10:57:56 C:\Windows\SYSTEM32\MPR.dll
6F740000 10/10/2017 10:57:56 C:\Windows\SYSTEM32\ntmarta.dll
70000000 07/16/2016 19:42:56 C:\Windows\SYSTEM32\samcli.dll
70020000 07/16/2016 19:42:50 C:\Windows\SYSTEM32\WINMMBASE.dll
70050000 07/16/2016 19:42:50 C:\Windows\SYSTEM32\WINMM.dll
70080000 10/10/2017 10:58:32 C:\Windows\SYSTEM32\WINSPOOL.DRV
703C0000 07/16/2016 19:42:50 C:\Windows\SYSTEM32\MSIMG32.DLL
72CF0000 10/10/2017 10:57:56 C:\Windows\SYSTEM32\bcrypt.dll
73B10000 07/16/2016 19:43:06 C:\Windows\SYSTEM32\VERSION.dll
73B30000 07/16/2016 19:42:56 C:\Windows\SYSTEM32\USERENV.dll
73B50000 07/16/2016 19:42:50 C:\Windows\System32\CRYPTBASE.dll
73B60000 10/10/2017 10:58:20 C:\Windows\System32\SspiCli.dll
73B80000 10/10/2017 10:58:22 C:\Windows\System32\KERNELBASE.dll
73D30000 07/16/2016 19:42:56 C:\Windows\System32\msvcp_win.dll
73E00000 10/10/2017 10:58:18 C:\Windows\System32\ole32.dll
73F90000 09/16/2016 01:37:06 C:\Windows\System32\WS2_32.dll
74000000 10/10/2017 10:58:04 C:\Windows\System32\ucrtbase.dll
740F0000 07/16/2016 19:42:48 C:\Windows\System32\clbcatq.dll
74180000 10/10/2017 10:58:18 C:\Windows\System32\KERNEL32.DLL
74260000 10/10/2017 10:58:08 C:\Windows\System32\USER32.dll
743C0000 10/10/2017 10:58:02 C:\Windows\System32\shcore.dll
74550000 10/10/2017 10:58:02 C:\Windows\System32\gdi32full.dll
746B0000 10/10/2017 10:58:02 C:\Windows\System32\MSCTF.dll
747F0000 07/16/2016 19:42:56 C:\Windows\System32\cfgmgr32.dll
74830000 07/16/2016 19:42:50 C:\Windows\System32\profapi.dll
74840000 10/10/2017 10:58:20 C:\Windows\System32\RPCRT4.dll
74910000 07/16/2016 19:42:50 C:\Windows\System32\msvcrt.dll
749D0000 10/10/2017 10:58:22 C:\Windows\System32\SHELL32.dll
75DB0000 07/16/2016 19:42:56 C:\Windows\System32\powrprof.dll
75E00000 08/02/2016 12:47:22 C:\Windows\System32\win32u.dll
75E20000 10/10/2017 10:58:20 C:\Windows\System32\sechost.dll
75E80000 07/16/2016 19:42:46 C:\Windows\System32\PSAPI.DLL
76240000 10/10/2017 10:58:26 C:\Windows\System32\bcryptPrimitives.dll
762A0000 10/10/2017 10:58:08 C:\Windows\System32\GDI32.dll
762D0000 07/16/2016 19:42:56 C:\Windows\System32\kernel.appcore.dll
762E0000 10/10/2017 10:57:56 C:\Windows\System32\combase.dll
76560000 10/10/2017 10:57:56 C:\Windows\System32\OLEAUT32.dll
76600000 10/10/2017 10:58:04 C:\Windows\System32\windows.storage.dll
76B70000 07/16/2016 19:42:50 C:\Windows\System32\advapi32.dll
76BF0000 07/16/2016 19:42:50 C:\Windows\System32\IMM32.DLL
77050000 07/16/2016 19:43:04 C:\Windows\System32\SHLWAPI.dll
770A0000 10/10/2017 10:58:26 C:\Windows\SYSTEM32\ntdll.dll
787E0000 10/10/2017 10:58:00 C:\Windows\AppPatch\AcGenral.DLL
==========================================
开头就详细列出了错误信息
在Unit1.cpp中第19行,申请了1字节的空间没有释放