麻烦大侠帮忙看一下这段代码是不是有内存泄露

other123othe 2014-09-10 04:27:07


CDC memDC;
CBitmap bitmap;
BITMAP bmp;
CClientDC dc(this);
OnPrepareDC(&dc);
memDC.CreateCompatibleDC(&dc);
bitmap.CreateCompatibleBitmap(&dc, m_pDoc->m_img.m_nWidth, m_pDoc->m_img.m_nHeight);
bitmap.GetBitmap(&bmp);
memDC.SelectObject(&bitmap);
SetPen(&memDC, m_fScale);
m_pDoc->m_img.Draw(memDC.m_hDC, 0, 0, m_pDoc->m_img.m_nWidth, m_pDoc->m_img.m_nHeight);

...全文
250 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
other123othe 2014-09-12
  • 打赏
  • 举报
回复
自己顶一下 自己顶一下 自己顶一下
-小仙- 2014-09-11
  • 打赏
  • 举报
回复
泄露与否在DEBUG模式下运行就不就知道了? CBitmap * pOldBmp = memDC.SelectObject(&bitmap); 会返回一个就得位图, 所有使用SelectObject选入DC的资源,都会返回旧的资源,最后要把旧的重新选入到DC,新创建的资源对象才会在析构时自动释放。 memDC.SelectObject(pOldBmp )
other123othe 2014-09-11
  • 打赏
  • 举报
回复
引用 11 楼 zhao4zhong1 的回复:
CreateCompatibleDC The CreateCompatibleDC function creates a memory device context (DC) compatible with the specified device. HDC CreateCompatibleDC( HDC hdc // handle to the device context ); Parameters hdc Handle to an existing device context. If this handle is NULL, the function creates a memory device context compatible with the application's current screen. Return Values If the function succeeds, the return value is the handle to a memory device context. If the function fails, the return value is NULL. Windows NT: To get extended error information, callGetLastError. Remarks A memory device context is a device context that exists only in memory. When the memory device context is created, its display surface is exactly one monochrome pixel wide and one monochrome pixel high. Before an application can use a memory device context for drawing operations, it must select a bitmap of the correct width and height into the device context. This may be done by using CreateCompatibleBitmap specifying the height, width, and color organization required in the function call. When a memory device context is created, all attributes are set to normal default values. The memory device context can be use as a normal device context. You can set the attributes to non-default values, obtain the current setting of its attributes, select pens, brushes and regions into it. The CreateCompatibleDC function can only be used with devices that support raster operations. An application can determine whether a device supports these operations by calling the GetDeviceCaps function. When you no longer need the memory device context, call the DeleteDC function to delete it. ICM: If the device context (DC) that is passed to this function through its hdc parameter is enabled for Independent Color Management (ICM), the DC created by the function is ICM-enabled. The source and destination color spaces are specified in the DC. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in wingdi.h. Import Library: Use gdi32.lib. See Also Device Contexts Overview, Device Context Functions, CreateCompatibleBitmap, DeleteDC, GetDeviceCaps
不认识英文
mayudong1 2014-09-11
  • 打赏
  • 举报
回复
你应该是确认这块代码有内存泄露后来问为什么有内存泄露,可能很多人就有兴趣了 直接这样问有没有。。。
赵4老师 2014-09-11
  • 打赏
  • 举报
回复
CreateCompatibleDC The CreateCompatibleDC function creates a memory device context (DC) compatible with the specified device. HDC CreateCompatibleDC( HDC hdc // handle to the device context ); Parameters hdc Handle to an existing device context. If this handle is NULL, the function creates a memory device context compatible with the application's current screen. Return Values If the function succeeds, the return value is the handle to a memory device context. If the function fails, the return value is NULL. Windows NT: To get extended error information, callGetLastError. Remarks A memory device context is a device context that exists only in memory. When the memory device context is created, its display surface is exactly one monochrome pixel wide and one monochrome pixel high. Before an application can use a memory device context for drawing operations, it must select a bitmap of the correct width and height into the device context. This may be done by using CreateCompatibleBitmap specifying the height, width, and color organization required in the function call. When a memory device context is created, all attributes are set to normal default values. The memory device context can be use as a normal device context. You can set the attributes to non-default values, obtain the current setting of its attributes, select pens, brushes and regions into it. The CreateCompatibleDC function can only be used with devices that support raster operations. An application can determine whether a device supports these operations by calling the GetDeviceCaps function. When you no longer need the memory device context, call the DeleteDC function to delete it. ICM: If the device context (DC) that is passed to this function through its hdc parameter is enabled for Independent Color Management (ICM), the DC created by the function is ICM-enabled. The source and destination color spaces are specified in the DC. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in wingdi.h. Import Library: Use gdi32.lib. See Also Device Contexts Overview, Device Context Functions, CreateCompatibleBitmap, DeleteDC, GetDeviceCaps
other123othe 2014-09-11
  • 打赏
  • 举报
回复
引用 9 楼 bojie5744 的回复:
不要重复进行此步骤,memDC.CreateCompatibleDC(&dc);
既然如此 你告诉我怎么修改哈 我只会new delete 这个绘图的这块 真心不知道哈
  • 打赏
  • 举报
回复
不要重复进行此步骤,memDC.CreateCompatibleDC(&dc);
  • 打赏
  • 举报
回复
若果你这段代码进行for循环操作,或者重复操作,则内存泄露.
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
检查是否资源泄漏的办法之一: 在任务管理器 进程 查看 选择列 里面选择:内存使用、虚拟内存大小、句柄数、线程数、USER对象、GDI对象 让你的程序(进程)不退出,循环执行主流程很多遍,越多越好,比如1000000次甚至无限循环,记录以上各数值,再隔至少一小时,越长越好,比如一个月,再记录以上各数值。如果以上两组数值的差较大或随时间流逝不断增加,则铁定有对应资源的资源泄漏!
...........你的时间挺多的
kuankuan_qiao 2014-09-11
  • 打赏
  • 举报
回复
赵老师 讲的不错
赵4老师 2014-09-11
  • 打赏
  • 举报
回复
检查是否资源泄漏的办法之一: 在任务管理器 进程 查看 选择列 里面选择:内存使用、虚拟内存大小、句柄数、线程数、USER对象、GDI对象 让你的程序(进程)不退出,循环执行主流程很多遍,越多越好,比如1000000次甚至无限循环,记录以上各数值,再隔至少一小时,越长越好,比如一个月,再记录以上各数值。如果以上两组数值的差较大或随时间流逝不断增加,则铁定有对应资源的资源泄漏!
肆水東澤 2014-09-10
  • 打赏
  • 举报
回复
这段代码,没看出来内存泄露 你就是画个图,dc是用类创建的对象,一般在析构的时候,都会释放掉dc的,不过还是手动释放下的好 建议你查下你的图数据有没有内存泄露的问题。 你在网上查下vld,用这个工具,可以定位到内存泄露的代码行,很方便,用API也可以定位,就是使用麻烦点
other123othe 2014-09-10
  • 打赏
  • 举报
回复
引用 1 楼 fqzlala 的回复:
最好release下dc
大侠啊 你不要最好哈 你能用确定以及肯定的态度告诉我要不要 releaseDC 以及如何 release DC
肆水東澤 2014-09-10
  • 打赏
  • 举报
回复
最好release下dc

16,470

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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