`
hunxiejun
  • 浏览: 1145152 次
文章分类
社区版块
存档分类
最新评论

object-c代码中是否需要一个safe release宏

 
阅读更多

翻译自:http://stackoverflow.com/questions/5068578/do-we-really-need-a-safe-release-macro

问:

很多人包括我自己使用类似这种的宏:

#define SAFE_RELEASE(X) [X release]; X = nil;

我在评估为什么我这样使用,并且说一下自己的观点。

我想使用这个宏的原因如果我们使用一个已经释放的对象,程序不会产生异常,因为当一个对象值为nil的时候,Object-C会忽略掉这个异常。

打击我的是这样做的话会产生一些隐晦的BUG。也许当再次使用X的时候程序崩溃掉是更好的选择,这样的话,测试的时候你可以发现这个BUG并改善你的代码。

这个宏是否鼓励lazy programing?

答1:

我想你在问题中说了正反两方面,所以我没有多少要补充的。我自己不使用这个结构。像你提议的那样,它可以能会掩饰掉不恰当的内存使用。我倾向于修正这个BUG而不是这种征兆。

因此,我有时看到的一种折中的方式是:

l 在开发期间令其崩溃

l 在最终产品代码中令var= nil

这种方式对于付费用户会更稳定可靠并且在开发期间同样会崩溃。

我同样不热衷于这种方式,因为面向用户的版本使用不同的代码只是为了使有BUG的版本保持运行,但这并不代表它在做正确的事情。没有崩溃但毁坏了你的数据库不是一个想要的行为。。。。。。

答2:

我也正在研究相同的问题。查阅一些资料后,我得到这样的方法:

#define DEBUGGING
//#definePRODUCTION
#ifdef DEBUGGING
#define SAFE_RELEASE(X) [X release];
#else
#define SAFE_RELEASE(X) [X release]; X = nil;
#endif

这样,如果我在开发阶段程序会崩溃,如果是最终产品代码则不这样。

分享到:
评论

相关推荐

    StereoIssues

    //CDXUTDialog这个类负责纪录一个对话框的所有属性以及它上面的所有控件信息 //CDXUTDialogResourceManager,这个类保存了所有注册过的对话框链表,以及这些对话框共享的资源 //CDXUTElement 这个类保存了需要渲染的...

    Addison.Wesley.The.Java.Programming.Language.4th.Edition.Aug.2005.chm

    you will find some major changes, both in the language and the book, since the 1.3 release that the third edition covered. There are new chapters on generics, enums, and annotationsthe major new ...

    A盾电脑防护

    //* 搞内核编程的不妨把它当成一个资料库 :) //* //************************************************************************************ 文件列表: I:. │ 3600safe.sln │ readme.txt │ ├─3600safe ...

    Swift.Programming.Nuts.and.Bolts.B01604F20C

    In addition knowledge of functional and/or object-oriented programming concepts, while not necessary, is also useful. It's also great for experienced Objective-C developers who want to quickly get up...

    Sakemail

    Not bug or enhancements release. Serge Wagener put me to work >:|1.8.4- Dmitry Bondarenko (again) has found a bug in wich I do not respect the RFC, wich say that replys from the SMTP server could be ...

    如何 IObjectSafety 标记 ATL 控件安全初始化

    需要用来获得所需的功能在步骤涉及到 IObjectSafetyImpl 用作您的控件派生的类之一,和重写 GetInterfaceSafetyOptions 和 SetInterfaceSafetyOptions。 这使您实现所需的功能在这种情况下意味着将标记为可安全编写...

    stdafx.h代码

    stdafx.h的代码// This is a part of the Microsoft Foundation Classes C++ library. // Copyright (C) 1992-1998 Microsoft Corporation // All rights reserved. // // This source code is only intended as a ...

    python3.6.5参考手册 chm

    PEP 442: Safe Object Finalization PEP 456: Secure and Interchangeable Hash Algorithm PEP 436: Argument Clinic Other Build and C API Changes Other Improvements Significant Optimizations ...

    Oracle PL/SQL programming(5th Edition)

    Thoroughly updated for Oracle Database 11g Release 2, this edition reveals new PL/SQL features and provides extensive code samples, ranging from simple examples to complex and complete applications, ...

    EurekaLog_7.5.0.0_Enterprise

    30)..Fixed: Leak of EurekaLog exception information object 31)..Fixed: Wrong chaining exceptions inside GetMem/FreeMem 32)..Fixed: Memory leak after low-level unhook of function 33)..Fixed: Re-...

    VclZip pro v3.10.1

    Much faster processing due to linking to Zlib object files for compression and decompression routines. Blocked Zip Files (spanned zip archives split onto hard drive) - Now completely compatible ...

    DbfDotNet_version_1.0_Source

    In contrast it is using TypeSafe records (without boxing/unboxing) and type safe emitted code. The code is emitted only once per table. It has therefore I believe the potential to be the fastest ...

    BobBuilder_app

    Twitter Digg Facebook Del.icio.us Reddit Stumbleupon Newsvine Technorati Mr....Database » Database » Other databasesLicence CPOL ...Even faster Key/Value store nosql embedded database ...

    spring-framework-reference-4.1.2

    XML shortcut with the c-namespace .......................................................... 46 Compound property names ....................................................................... 46 Using...

    spring-framework-reference4.1.4

    XML shortcut with the c-namespace .......................................................... 46 Compound property names ....................................................................... 46 Using...

Global site tag (gtag.js) - Google Analytics