juce中的引用计数
这个类提供了最基本的引用计数管理,界面库中,经常都需要消息发送,而带来的后果就是不知道消息中包含的对象是否还存在,如果不能很好管理的话就容易出现访问销毁了的对象这样的情况,所以,juce的界面无素也基于引用计数是个不错的选择
#ifndef JUCE_REFERENCECOUNTEDOBJECT_H_INCLUDED
#define JUCE_REFERENCECOUNTEDOBJECT_H_INCLUDED //==============================================================================
/**
A base class which provides methods for reference-counting. To add reference-counting to a class, derive it from this class, and
use the ReferenceCountedObjectPtr class to point to it. e.g. @code
class MyClass : public ReferenceCountedObject
{
void foo(); // This is a neat way of declaring a typedef for a pointer class,
// rather than typing out the full templated name each time..
typedef ReferenceCountedObjectPtr<MyClass> Ptr;
}; MyClass::Ptr p = new MyClass();
MyClass::Ptr p2 = p;
p = nullptr;
p2->foo();
@endcode Once a new ReferenceCountedObject has been assigned to a pointer, be
careful not to delete the object manually. This class uses an Atomic<int> value to hold the reference count, so that it
the pointers can be passed between threads safely. For a faster but non-thread-safe
version, use SingleThreadedReferenceCountedObject instead. @see ReferenceCountedObjectPtr, ReferenceCountedArray, SingleThreadedReferenceCountedObject
*/
class JUCE_API ReferenceCountedObject
{
public:
//==============================================================================
/** Increments the object's reference count. This is done automatically by the smart pointer, but is public just
in case it's needed for nefarious purposes.
*/
void incReferenceCount() noexcept
{
++refCount;
} /** Decreases the object's reference count.
If the count gets to zero, the object will be deleted.
*/
void decReferenceCount() noexcept
{
jassert (getReferenceCount() > 0); if (--refCount == 0)
delete this;
} /** Decreases the object's reference count.
If the count gets to zero, the object will not be deleted, but this method
will return true, allowing the caller to take care of deletion.
*/
bool decReferenceCountWithoutDeleting() noexcept
{
jassert (getReferenceCount() > 0);
return --refCount == 0;
} /** Returns the object's current reference count. */
int getReferenceCount() const noexcept { return refCount.get(); } protected:
//==============================================================================
/** Creates the reference-counted object (with an initial ref count of zero). */
ReferenceCountedObject() {} /** Destructor. */
virtual ~ReferenceCountedObject()
{
// it's dangerous to delete an object that's still referenced by something else!
jassert (getReferenceCount() == 0);
} /** Resets the reference count to zero without deleting the object.
You should probably never need to use this!
*/
void resetReferenceCount() noexcept
{
refCount = 0;
} private:
//==============================================================================
Atomic <int> refCount; friend struct ContainerDeletePolicy<ReferenceCountedObject>;
JUCE_DECLARE_NON_COPYABLE (ReferenceCountedObject)
};
juce中的引用计数的更多相关文章
- swift内存管理中的引用计数
在swift中,每一个对象都有生命周期,当生命周期结束会调用deinit()函数进行释放内存空间. 观察这一段代码: class Person{ var name: String var pet: P ...
- (20)Cocos2d-x中的引用计数(Reference Count)和自动释放池(AutoReleasePool)
引用计数 引用计数是c/c++项目中一种古老的内存管理方式.当我8年前在研究一款名叫TCPMP的开源项目的时候,引用计数就已经有了. iOS SDK把这项计数封装到了NSAutoreleasePool ...
- Objective-C中的引用计数
导言 Objective-C语言使用引用计数来管理内存,也就是说,每个对象都有个可以递增或递减的计数器.如果想使某个对象继续存活,那就递增其引用计数:用完了之后,就递减其计数.计数为0,就表示没人关注 ...
- Python中的引用计数法
目录 引用计数法 增量操作 计数器溢出的问题 减量操作 终结器 插入计数处理 引用计数法 增量操作 如果对象的引用数量增加,就在该对象的计数器上进行增量操作.在实际中它是由宏Py_INCREF() 执 ...
- java中垃圾回收机制中的引用计数法和可达性分析法(最详细)
首先,我这是抄写过来的,写得真的很好很好,是我看过关于GC方面讲解最清楚明白的一篇.原文地址是:https://www.zhihu.com/question/21539353
- cocos2d-x-3.3rc2-003 cocos中的引用计数和自己主动释放池
点击打开链接
- 深入理解 PHP7 中全新的 zval 容器和引用计数机制
深入理解 PHP7 中全新的 zval 容器和引用计数机制 最近在查阅 PHP7 垃圾回收的资料的时候,网上的一些代码示例在本地环境下运行时出现了不同的结果,使我一度非常迷惑. 仔细一想不难发现问题所 ...
- c语言模拟实现oc引用计数
#include<stdio.h> #include<stdlib.h> //在c中引入 引用计数机制 // 要解决的问题: 1,指向某块动态内存的指针有几个? // ...
- ARC————自动引用计数
一.内存管理/引用计数 1.引用计数式内存管理的方式(下面四种) 对象操作 OC方法 生成并持有对象 alloc/new/copy/mutableCopyd等方法 持有对象 retain方法 释放对象 ...
随机推荐
- paip.oracle query export to insert sql
paip.oracle query export to insert sql 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http:/ ...
- 分享一个用安卓手机就能引导pc安装linux系统办法
1.首先安卓手机下载软件DriveDroid.apk http://pan.baidu.com/s/1qW4pbT6 2.下载linux镜像文件放手机存储卡存储,放到Download/images/以 ...
- CMS(Concurrent Mark-Sweep)
CMS(Concurrent Mark-Sweep)是以牺牲吞吐量为代价来获得最短回收停顿时间的垃圾回收器.对于要求服务器响应速度的应用上,这种垃圾回收器非常适合.在启动JVM参数加上-XX:+Use ...
- 从一个非开发人员转行silverlight满一年的工作总结(第一次发帖)
自2013年3月进入公司到现在已整整一年.这一年,让我从一个大学毕业就去参军并且专业还不对口的大学生步入了软件开发这个高门槛行业.说实话,我真的很庆幸,庆幸遇到了两位赏识自己的领导从很多专业对口.能力 ...
- 3D 转换
用X.Y.Z分别表示空间的3个维度,三条轴互相垂直.如下图 1.左手坐标系 2.透视(perspective) 电脑显示屏是一个2D平面,图像之所以具有立体感(3D效果),其实只是一种视觉呈现 ,通过 ...
- 3D转换
CSS3 允许您使用 3D 转换来对元素进行格式化. 在本章中,您将学到其中的一些 3D 转换方法: 1. rotateX() 2. rotateY() <!DOCTYPE HTML>&l ...
- iOS切换window根控制器 (转)
转自linfengwenyou 在运行过程中更改根控制器的方法:(假设:A为当前根控制器,B为要设的根控制器) 方法一: 1. appdelegate.m中 self.window = [[UIWin ...
- 解决OpenWrt多拨刚开机拨号只拨上一次问题
红色标注为需要权限755/etc/ppp/ip-up.d/ip-up: 一旦 PPP 连结建立后, pppd 会找寻 /etc/ppp/ip-up 指令稿 如果这个指令稿存在并且可以执行的话,那么 P ...
- Spring-----事务配置的五种方式
转载自:http://blog.csdn.net/hekewangzi/article/details/51712821
- golang Rsa
package models import ( "crypto/rand" "crypto/rsa" "crypto/x509" " ...