记WIN32使用duilib时添加MFC类库发现内存泄露的问题解决

本来是需要在win32下使用duilib写一个界面,但后来发现需要使用MFC类库,就参考了下面这位仁兄的博客,在win32下配置MFC类库的支持,博客地址如下:《在Win32应用程序中使用MFC类库(转)

http://blog.csdn.net/susubuhui/article/details/9729645

win32SDK是不会检测内存泄露的,但是MFC是支持的,然后我就发现我的内存泄露问题了,本来MFC会在OutPut窗口打印出在哪一行发生了内存泄露:

如下:

但是,我因为不是MFC程序,显示为类似下面这样,

Detected memory leaks!
Dumping objects ->
normal block at 0x005B0430, 24 bytes long.
 Data: <(+W  &X   O     > 28 2B 57 01 B0 26 58 00 F0 ED 4F 00 CD CD CD CD

 

并没有定位在哪一行,这就尴尬了。

发现下面这个博客可以用来侦测,

http://blog.sina.com.cn/s/blog_4bf793ad0100jzrn.html

但是我懒省事,直接在stdafx.h里加上如下代码:

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

但是发现以有新的问题,问题是这样:

1>c:\program files (x86)\windows kits\8.1\include\um\gdiplusbase.h(30): error C2059: 语法错误:“字符串”
1>c:\program files (x86)\windows kits\8.1\include\um\gdiplusbase.h(31): error C2091: 函数返回函数
1>c:\program files (x86)\windows kits\8.1\include\um\gdiplusbase.h(31): error C2802: 静态成员“operator new”没有形参
1>c:\program files (x86)\windows kits\8.1\include\um\gdiplusbase.h(31): error C2333: “Gdiplus::GdiplusBase::operator new”: 函数声明中有错误;跳过函数体
1>c:\program files (x86)\windows kits\8.1\include\um\gdiplusbase.h(38): error C2059: 语法错误:“字符串”
1>c:\program files (x86)\windows kits\8.1\include\um\gdiplusbase.h(38): error C2090: 函数返回数组
1>c:\program files (x86)\windows kits\8.1\include\um\gdiplusbase.h(40): error C2059: 语法错误:“return”
1>c:\program files (x86)\windows kits\8.1\include\um\gdiplusbase.h(40): error C2238: 意外的标记位于“;”之前
1>c:\program files (x86)\windows kits\8.1\include\um\gdiplusbase.h(42): error C2143: 语法错误 : 缺少“;”(在“}”的前面)

问题及原因,还有解决办法,看下面这位仁兄给出的答案:

http://blog.csdn.net/pjl1119/article/details/6186167

然后终于解决,发现在是因为我用的duilib的UIDelegate.h的这一行:virtual CDelegateBase* Copy() const { return new CDelegate(*this); }

这样的话,就找到原因了

最后,感谢以上各位分享的博客。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
duilib 是一个开源的界面库,它提供了丰富的界面控件和布局管理功能,非常适合用于开发Windows桌面软件。而MFC(Microsoft Foundation Class)是微软开发的用于Windows平台的C++类库,提供了大量的封装好的功能和控件,方便开发人员进行Windows应用程序的开发。 在duilib使用MFC的PictureControl控件,需要先在项目中引入MFC库,并创建一个MFC应用程序。然后,在duilib的界面布局文件中使用窗口控件,并设置控件类型为"Pic",通过name属性给控件命名。 接下来,在MFC应用程序中,创建一个继承自duilib::CPaintManagerUI的类,重写其中的虚函数Notify,用于监听控件的消息事件。在Notify函数中,通过duilib的API获取到PictureControl控件的指针,并调用MFC的相关函数,实现图片的加载和显示。 具体代码如下所示: 在duilib界面布局文件(XML)中添加如下代码: ```xml <Control name="picControl" type="Pic" pos="10, 10, 100, 100" /> ``` 在MFC应用程序的窗口类中添加如下代码: ```cpp class CMainWnd : public CDialogEx { public: CMainWnd(CWnd* pParent = nullptr) : CDialogEx(IDD_MAIN_DIALOG, pParent) { } protected: virtual void DoDataExchange(CDataExchange* pDX) override { CDialogEx::DoDataExchange(pDX); } afx_msg void OnPaint() { CPaintDC dc(this); // 获取duilib的PictureControl控件指针 duilib::CPictureUI* pPicControl = (duilib::CPictureUI*)GetDlgItem(IDC_DUI_PIC); if (pPicControl == nullptr) return; // 获取MFC的CDC对象 CDC* pDC = CDC::FromHandle(dc.GetSafeHdc()); // 加载图片 CImage image; image.Load(_T("path_to_your_image")); // 绘制图片 image.Draw(pDC->GetSafeHdc(), pPicControl->GetPos().left, pPicControl->GetPos().top, pPicControl->GetPos().Width(), pPicControl->GetPos().Height()); } DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(CMainWnd, CDialogEx) ON_WM_PAINT() END_MESSAGE_MAP() ``` 以上就是在duilib使用MFC的PictureControl控件的步骤,通过上述代码,可以实现在duilib界面中显示MFC加载的图片。当然,在实际的项目中,可能还需要添加图片缩放、鼠标事件等处理逻辑,具体根据实际需求进行扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不识君的荒漠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值