MFC资源的释放

SKATE11 2013-03-20 05:48:47
在MFC里面的资源 比如说bitmap brush等等这些东西 微软为什么不直接在其类的析构函数里面delete掉 还麻烦我们每次用完之后代码删除 求解
...全文
280 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-03-21
  • 打赏
  • 举报
回复
Graphic Objects Home | Overview | How Do I | Tutorial Windows provides a variety of drawing tools to use in device contexts. It provides pens to draw lines, brushes to fill interiors, and fonts to draw text. MFC provides graphic-object classes equivalent to the drawing tools in Windows. The table below shows the available classes and the equivalent Windows graphics device interface (GDI) handle types. The general literature on programming for the Windows GDI applies to the Microsoft Foundation classes that encapsulate GDI graphic objects. This article explains the use of these graphic-object classes: Classes for Windows GDI Objects Class Windows handle type CPen HPEN CBrush HBRUSH CFont HFONT CBitmap HBITMAP CPalette HPALETTE CRgn HRGN Each graphic-object class in the class library has a constructor that allows you to create graphic objects of that class, which you must then initialize with the appropriate create function, such as CreatePen. Each graphic-object class in the class library has a cast operator that will cast an MFC object to the associated Windows handle. The resulting handle is valid until the associated object detaches it. Use the object’s Detach member function to detach the handle. The following code casts a CPen object to a Windows handle: CPen myPen; myPen.CreateSolidPen( PS_COSMETIC, 1, RGB(255,255,0) ); HPEN hMyPen = (HPEN) myPen; Process of Creating a Graphic Object in a Device Context The following four steps are typically used when you need a graphic object for a drawing operation: Define a graphic object on the stack frame. Initialize the object with the type-specific create function, such as CreatePen. Alternatively, initialize the object in the constructor. See the discussion of one-stage and two-stage creation, which provides example code. Select the object into the current device context, saving the old graphic object that was selected before. When done with the current graphic object, select the old graphic object back into the device context to restore its state. Allow the frame-allocated graphic object to be deleted automatically when the scope is exited. Note If you will be using a graphic object repeatedly, you can allocate it once and select it into a device context each time it is needed. Be sure to delete such an object when you no longer need it. What do you want to know more about? One-stage and two-stage construction of graphic objects Example of constructing a pen in one and two stages Example of selecting a pen into a device context Device contexts
ForestDB 2013-03-21
  • 打赏
  • 举报
回复
跟效率有关,这些要反复用的。 你来来回回的new,delete,系统承受不起。
  • 打赏
  • 举报
回复
微软很多函数都是C支持的,C里面是没有析构函数的概念的。
  • 打赏
  • 举报
回复
系统不会帮你做这些事情,在程序退出的时候,系统会回收所有的资源。但是一个程序还在运行,只要资源没有被释放,系统就会认为这个资源在后面都会用到。而事实是,你每次用的时候,都new了一个新的。这些破烂就不断的堆积,吃着内存。
xiaolomg 2013-03-21
  • 打赏
  • 举报
回复
引用 7 楼 SKATE11 的回复:
引用 6 楼 boyhailong 的回复:如果太懒就有智能指针都封装好多东西了 偏偏没把delete这个功能放进析构函数里面 有点想不通 是不是另有原因
放进析构里,如果是new出来的,还不是得你自己负责?
SKATE11 2013-03-21
  • 打赏
  • 举报
回复
还有官方点的回答吗
SKATE11 2013-03-20
  • 打赏
  • 举报
回复
我想是不是因为句柄在随时变化的问题
SKATE11 2013-03-20
  • 打赏
  • 举报
回复
引用 6 楼 boyhailong 的回复:
如果太懒就有智能指针
都封装好多东西了 偏偏没把delete这个功能放进析构函数里面 有点想不通 是不是另有原因
xiaolomg 2013-03-20
  • 打赏
  • 举报
回复
如果太懒就有智能指针
Qyee16 2013-03-20
  • 打赏
  • 举报
回复
就像java一样,替你做太多东西,程序员就不自由了。
SKATE11 2013-03-20
  • 打赏
  • 举报
回复
引用 2 楼 chuachua66 的回复:
培养你良好习惯呗,自己申请的要自己释放。:) 其实handle都有引用计数,感觉是可以自动释放的,难道最初那哥们没这么写,以后改的成本太高,就一直延续下去了?
感觉成本也不是太高啊 就一个析构函数而已
  • 打赏
  • 举报
回复
引用 2 楼 chuachua66 的回复:
培养你良好习惯呗,自己申请的要自己释放。:) 其实handle都有引用计数,感觉是可以自动释放的,难道最初那哥们没这么写,以后改的成本太高,就一直延续下去了?
原来是这样、、、
chuachua66 2013-03-20
  • 打赏
  • 举报
回复
培养你良好习惯呗,自己申请的要自己释放。:) 其实handle都有引用计数,感觉是可以自动释放的,难道最初那哥们没这么写,以后改的成本太高,就一直延续下去了?
SKATE11 2013-03-20
  • 打赏
  • 举报
回复
自己顶上去 ++

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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