LoadLibrary内存泄露

bingyu_2008 2010-09-19 05:08:14
我使用LoadLibrary方法加载一个资源文件“rc.dll”,结果用CodeGuard竟然检查出有内存泄露,当把LoadLibrary那行代码注释掉的时候就没有内存泄露了,不知道什么原因,各位帮忙分析一下,我的调用方式:
//.h文件中的声明
HINSTANCE FRcDll

//.cpp中的实现
TRcString::TRcString(LPCSTR szFile)
{
FRcDll = ::LoadLibrary(szFile);
}

TRcString::~TRcString()
{
if (NULL != FRcDll)
{
::FreeLibrary(FRcDll);
FRcDll = NULL;
}
}


以下是错误信息
Error 00120. 0x300010 (Thread 0x0100):
Resource leak: The memory block (0x15ADB50) was never freed

The memory block (0x015ADB50) [size: 200 bytes] was allocated with SysGetMem
Call Tree:
0x01925BF8
0x01925C51
0x01927E8C
0x0192779D
0x01927DE7
0x0192ACB6
...全文
439 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
bingyu_2008 2010-09-21
  • 打赏
  • 举报
回复
给大家看下代码

//---------------------------------------------------------------------------
//Form的代码
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
private: // User declarations
HINSTANCE FRcDll;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
virtual __fastcall ~TForm1();
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "TRcString.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
FRcDll = ::LoadLibrary("M:\\mts\\mtsu\\bin\\yqcln_pbchs.dll");
//TRcString rc;
}
//---------------------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
::FreeLibrary(FRcDll);
}
//---------------------------------------------------------------------------


//TRcString的代码
//---------------------------------------------------------------------------
#ifndef TRcStringH
#define TRcStringH
//---------------------------------------------------------------------------
#include <windows.h>
//---------------------------------------------------------------------------
class TRcString
{
public:
TRcString();
TRcString(const char* szRcFile);
virtual ~TRcString();

private:
HINSTANCE FRcDll;
};
//---------------------------------------------------------------------------
extern TRcString* RcString;
//---------------------------------------------------------------------------
#endif//TRcStringH



//---------------------------------------------------------------------------
#include "TRcString.h"
//---------------------------------------------------------------------------
TRcString* RcString = NULL;
//---------------------------------------------------------------------------
TRcString::TRcString(const char* szRcFile)
{
FRcDll = ::LoadLibrary(szRcFile);
}
//---------------------------------------------------------------------------
TRcString::TRcString()
{
FRcDll = ::LoadLibrary("M:\\mts\\mtsu\\bin\\yqcln_pbxchs.dll");
}
//---------------------------------------------------------------------------
TRcString::~TRcString()
{
if (NULL != FRcDll)
{
::FreeLibrary(FRcDll);
FRcDll = NULL;
}
}
//---------------------------------------------------------------------------



新建个工程,把这两个文件加进去,把内存检查工具打开后运行,然后再把Form里面被注释的代码释放掉运行,我是真的找不到内存泄露的原因了,请大家帮忙看看
科多兽 2010-09-19
  • 打赏
  • 举报
回复
期待中.....
bingyu_2008 2010-09-19
  • 打赏
  • 举报
回复
很奇怪,我原来建的工程里面就不会出现这种情况,源文件和dll都是直接拷贝到新工程里面的,同时出现的还有另外一个问题,memcpy(&_buffer[index], &input[i], length - i);运行到这句的时候报下面一个错误pointer underrun,这个是网上下的一个md5文件里面的

Error 00139. 0x140100 (Thread 0x0250):
Pointer underrun: 0x0012F7F0, that is at offset 8 in local block
0x0012F7E8(=[ebp-0x14] @mtsuprj.exe:0x01:0015CD) (size 8).
memcpy(0x0012F878, 0x0012F7F0, 0x0 [0])
zzbinfo 2010-09-19
  • 打赏
  • 举报
回复
支持楼上,看你的dll本身是不是就有问题
周药师 2010-09-19
  • 打赏
  • 举报
回复
是不是你的DLL里本身就泄漏?
bingyu_2008 2010-09-19
  • 打赏
  • 举报
回复
不加也是这样的,FreeLibrary(FRcDll);是执行过的,还是会报内存泄露
CppFile 2010-09-19
  • 打赏
  • 举报
回复
if (NULL != FRcDll)
不加这个看看

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧