2022年使用QT进行内存管理收藏定义 .pdf
《2022年使用QT进行内存管理收藏定义 .pdf》由会员分享,可在线阅读,更多相关《2022年使用QT进行内存管理收藏定义 .pdf(6页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Memory Management with Qt 使用 QT 进行内存管理Garbage Collection through the Back Door 通过后门垃圾回收Highly typed programming languages demand explicit (or at least implicit) creationof objects mentioning their types. They differ in how the objects are deleted: there are two approaches - direct deletion by the pr
2、ogram or garbage collection by the language. 高级类型的语言要求对象创建时明确的(或者至少是隐式的)声明他们的类型。他们可以通过对象是如何销毁的来区别:有两种方法通过程序直接销毁或者通过语言的垃圾回收机制。Explicitly deleting languages (eg. C+, Pascal) demand that the user knows when it is best to get rid of a dynamically created object. Garbage collected languages (eg. Java, Sm
3、allTalk) try to find out when an object is no longer used and automatically reclaim the memory. 明确的删除的语言 (如,C+,Pascal)要求用户知道什么时候最好避免动态创建的对象。垃圾回收的语言(如 java,Smaltalk)尝试找出什么时候一个对象不再被使用并自动的回收那部分的内存。Explicit deletion has the nice property that the programmer is in full control over the programs ressource
4、s - the amount of memory used and the amount and timingof CPU cycles spent on managing these ressources. 明确的删除的好处那就是程序员可以完全控制程序或者资源使用的内存的大小和管理这些资源花费的 cpu 周期的大小和数量。Garbage Collection on the other hand takes a lot of responsibility off the programmer by keeping track on object references inside the sy
5、stem and freeing memory as needed. 另一方面垃圾回收通过在系统内部维持对象的属性的轨迹并且在必要时释放内存来分担程序员的一部分的职责Default C+ Memory Handling 默认的 C+ 的内存处理C+ is a language that demands that the user deletes objects explicitly. There arethree important strategies on how to handle this: C+ 是一种要求用户显式的删除对象的语言。在如何处理方面有三种重要的策略:Let the cr
6、eating object delete its child objects. Let the last object to handle an object delete it. Dont care about memory and forget about it. 让创建的对象删除它的子对象。让最后的对象来控制一个对象来删除它。不关心内存并且忘了有这件事。The last strategy is called memory leak and usually regarded as a bug. 最后的策略被称为内存泄露并且常常被认为是一个bugThe real problem with C
7、+ is to find out which of the first two strategies is right and how to implement it. There are some cases in which the creating object is deleted much earlier than the child object and in which it is hard to find out which is名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - -
8、 - 第 1 页,共 6 页 - - - - - - - - - the last object to handle it. C+ 中的真正的问题是找出前两种策略的哪个是正确的并且如何实现它。在一些情况中创建的对象比子对象删除的早的多并且很难找出哪个是最后的对象来处理它Default Qt Memory Handling 默认的 QT 内存处理Qt maintains hierarchies of objects. For widgets (visible elements) these hierarchies represent the stacking order of the widge
9、ts themselves, for non-widgets they merely express ownership of objects. Qt deletes child objects together with their parent object. While this takes care of 90% of memory handling problems it also brings some challenges and leaves a few aspects open. Qt 维护了对象的分类。对widget(可视化元件)这些层次表现在widgets的压栈顺序,对非
10、widget 的他们很少表现出对象的归属。Qt 把子对象和他们的父对象一起删除。这样能够处理90% 的内存处理问题同时它也带来了一些机会并且一些方面的敞开。QPointer QPointer is a template class that can watch a dynamic object and updates when the object is deleted, otherwise it behaves just like a normal pointer: Qpointer是一个模版类,它能够监控动态对象并且在对象被删除时更新,否则它和一般的指针的表现几乎是一样的。QDate *m
11、ydate=new QDate(QDate:currentDate(); QPointer mypointer=mydata; mydate-year(); / - 2005 mypointer-year(); / - 2005 After deletion of the original object it starts to behave differently: 在删了原对象之后他就开始有不同的表现了delete mydate; if(mydate=0) printf(clean pointer); else printf(dangling pointer); / - dangling
12、pointer if(mypointer.isNull() printf(clean pointer); else printf(dangling pointer); / - clean pointer QObjectCleanupHandler The cleanup handler of Qt is a further step towards automatic garbage collection. It can register several child objects and delete them all simultaniously when ititself is dele
13、ted. It also recognises automatically if any of its registered objects isdeleted earlier and removes it from its list. 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 6 页 - - - - - - - - - Qt 中的 cleanup句柄是自动垃圾回收中走的更远的一步。它能够注册多个的子对象并且在他被删除的同时删除它们。它同样可以自动的发现任何他的注册
14、的对象被提前删除并且从他的表中删除。This class can be used any time when objects that are not in the same hierarchy need to be deleted upon a certain event (eg. several top level windows need to be closed when a button is pressed or when another window closes). 这个类可以在不在同一层次的对象因为一个特殊的事件(如当一个按钮被按下或者当另一个窗口被关闭的时候,几个顶层的窗口
15、需要被关闭)被删除的任何时候使用. /instantiate handler QObjectCleanupHandler *cleaner=new QObjectCleanupHandler; /create some windows QPushButton *w; w=new QPushButton(Remove Me); w-show(); /register first button cleaner-add(w); /if first button is pressed, it removes itself connect(w,SIGNAL(clicked(),w,SLOT(delete
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年使用QT进行内存管理收藏定义 2022 使用 QT 进行 内存 管理 收藏 定义
限制150内