2008年7月25日星期五

使用HookLogger查找程序中的内存泄漏

在程序开发过程中难免会遇到内存泄漏问题。程序安装到设备上,如果使用时间一长便会提示内存已满,程序退出!!

当然最好是在程序开发的过程中保持良好的设计和习惯,尽量保证谁创建谁释放!

1.如何判断程序中时候存在内存泄漏:
在模拟器上启动程序后尽量运行所有功能,然后从程序的退出口退出(注意不是,在IDE上结束程序也不是关闭模拟器)。这时候如果程序在无任何异常的情况下 退出,那么恭喜你。你的程序在你运行的过程中没有内存泄漏,但不要高兴的太早。要把所有功能的运行完,并多次运行测试。才能保证你的程序没有内存泄漏。如 果退出时出现异常,很不幸,你的程序存在内存泄漏。

2.查找内存泄漏的代码
简单的代码我们可以看代码查找打内存泄漏的问题。大部分的问题都是在一个函数内把参数alloc。而没有释放原来的内存。这样就是一处。
复杂的代码我们可以使用HOOKLOGGER.
网上到处都是下载一个。我用的是 0.42 rc4版本的。
(1)安装及其简单:安装在和SYMBIAN同一个盘符下,注意路径可以随便,但路径的文件夹名称中不能有空格。
我的安装在了 c://symbian 目录下了。
(2)以上步骤完成后还不能使用。在安装路径下找到 HookLogger.chm 这个文件打开看看

Set up Hooks on an SDK
Run "SetupHooks.cmd". It has this syntax:

SetupHooks [--remove|r] [--platform=] [--build=]

where:
specifies the device name to attach hooks to (as listed by 'devices'),
or DEFAULT for the default device, or EPOCROOT to attach to %EPOCROOT%\epoc32
(as used by legacy Symbian tools)
defaults to WINSCW
defaults to UDEB
--remove detaches the hooks, restoring the original EUSER.DLL

where only DEVICE is mandatory (square brackets denote optional args). So to setup HookLogger on an SDK you've given the alias 'NewVer' it's simply:

SetupHooks NewVer

Note that HookLogger no longer supports EKA1 emulators, i.e. Symbian OS v8.1a and earlier. Also note that although you can try =UREL the results are usually far less satisfactory (optimisations hamper call stack reading, symbols are often missing).

To detach the hooks from an SDK run the same command but adding --remove or --r:

SetupHooks NewVer --remove

When the hooks are attached to an SDK this means that its EUSER.DLL is modified to statically import a "parasite" DLL which contains the hooking code, and the original EUSER.DLL is kept aside renamed as EUSER.orig.DLL. If you delete the parasite DLL then you'll get a "Can't find …DLL" Windows error when launching the emulator; you can either restore the original EUSER.DLL manually or re-get the SDK (e.g. if using Component Based Releases then getenv again)


E文不好的查查什么意思。

打开“开始”菜单中的“运行” 输入 :
cmd
//进入命令行模式
devices
//这个命令可以查看我们的开发机器上安装了几个symbian SDK
//S60_3rd_MR_2:com.nokia.s60 - default
//S60_2nd_FP3:com.nokia.series60
//S60_3rd_FP1:com.nokia.s60
//S60_3rd_MR_2:com.nokia.s60 - default 这个说明默认的SDK
//我们也可以执行 一下命令来更改 默认的 SDK
devices -setdefault @S60_2nd_FP3:com.nokia.series60
//如果不修改就不用执行了
//接下来我们给HOOKLOGGER 设置SDK,执行如下命令:
SetupHooks S60_3rd_MR_2:com.nokia.s60
//这样在 这个 SDK 下启动模拟器时 HOOKLOGGER 会自动启动
//好了,安装和配置已经搞定了。没其他地方说的那么”困难“改这改那。头都晕了!!!
(3)调试代码
先启动HOOKLOGGER 然后启动程序。例如我的代码:
void CHookLoggerDemoAppUi::HandleCommandL(TInt aCommand)
{
switch (aCommand)
{
case EEikCmdExit:
case EAknSoftkeyExit:
Exit();
break;

case ECommand1:
{

// Load a string from the resource file and display it
//HBufC* textResource = StringLoader::LoadLC(R_COMMAND1_TEXT);
HBufC* textResource = _L("this is message").AllocL();
CAknInformationNote* informationNote;

informationNote = new ( ELeave ) CAknInformationNote;

// Show the information Note with
// textResource loaded with StringLoader.
informationNote->ExecuteLD( *textResource);

// Pop HBuf from CleanUpStack and Destroy it.
// CleanupStack::PopAndDestroy(textResource);
}
break;
这个代码是AppUi HandleCommandL 处理菜单时间的函数。
我们注释了 CleanupStack::PopAndDestroy(textResource); 让其HBufC 不释放。

然后我们启动程序。(注意如果你的程序不执行这段代码,HOOKLOGGER 不会发现内存泄漏,所以找内存泄漏一定要运行相关功能)

程序启动后我们进入菜单运行 ECommand1 功能。从程序的退出口或退出菜单,退出程序。这时候 IDE会出现 类似的错误:
No source available for "0x49FBD6D( ekern.exe )() "







这时候我们按F8 (carbide) 。这是我们可以看到模拟器上弹出的对话框信息
如图:















再看 HOOKLOGGER 界面,打开 heap 页,然后点击 下面的 ”check“按钮:
如图:













双击选中项。会弹出如图界面:











双击选择行:

便可看到内存泄漏的代码了。












好了剩下的就是你修改代码了。


这个代码简单了点,如果是复杂的代码也是这个步骤!!!

没有评论: