ACE_Singleton内存管理

本文介绍了ACE框架提供的Singleton模式实现,并对比了ACE_Singleton与ACE_Unmanaged_Singleton的区别及应用场景。指出ACE_Singleton适用于一般程序,而ACE_Unmanaged_Singleton更适合DLL环境中使用。此外,还详细解释了如何利用ACE_Cleanup进行资源清理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ACE提供了Singleton模式的TEMPLATE实现,近期项目中用了一下ACE_Singleton,一开始是照抄C++NP2中的,如,ACE_Unmanaged_Singleton<TP_Logging_Task, ACE_Null_Mutex> TP_LOGGING_TASK;后面进入代码发现,
ACE_Unmanaged_Singleton一般用在DLL中,比如在服务配置模式中使用,而在一般程序中,还是使用ACE_Singleton,
定义如下:
template <class TYPE, class ACE_LOCK>
class ACE_Singleton : public ACE_Cleanup
从ACE_Cleanup继承有个好处就是不用自己delete所创建的Singleton对象,ACE框架在程序退出的时候,将自动帮你删除这个对象。
这是通过ACE_Object_Manager来实现的,对应用是透明的,
* Register an ACE_Cleanup object for cleanup at process
   * termination.  The object is deleted via the
   * <ace_cleanup_destroyer>.  If you need more flexiblity, see the
   * <other at_exit> method below.  For OS's that do not have
   * processes, cleanup takes place at the end of <main>.  Returns 0
   * on success.  On failure, returns -1 and sets errno to: EAGAIN if
   * shutting down, ENOMEM if insufficient virtual memory, or EEXIST
   * if the object (or array) had already been registered.
   */
  static int at_exit (ACE_Cleanup *object, void *param = 0);
如果你使用的是ACE_Unmanaged_Singleton,则必须自己调用close函数删除对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值