iPhone对象检查释放

发布于 2024-11-02 13:22:42 字数 494 浏览 1 评论 0原文

我想检查一个对象是否有一些计数这是我的测试代码

NSMutableArray  *array=[[NSMutableArray alloc]init];

    if(array)
    {
        NSLog(@"hiiiiiii"); 
    }

CASE-2

NSMutableArray  *array=[[NSMutableArray alloc]init];
    [array release];
    if(array)
    {
        NSLog(@"hiiiiiii"); 
    }

在这两种情况下我得到与打印的“hiiiiiii”相同的输出。

谁能告诉我如何检查我的对象是否需要释放或已经释放。

我知道我应该跟踪我的对象的计数器,但我正处于代码过于复杂的阶段,我需要帮助..

请帮助..

还要告诉苹果允许多少内存泄漏?

I want to check if an object has some count or not Here is my testing code

NSMutableArray  *array=[[NSMutableArray alloc]init];

    if(array)
    {
        NSLog(@"hiiiiiii"); 
    }

CASE-2

NSMutableArray  *array=[[NSMutableArray alloc]init];
    [array release];
    if(array)
    {
        NSLog(@"hiiiiiii"); 
    }

Here in both cases i got same output as printed "hiiiiiii".

Can anyone tell me how will i check if my object need to release or already released.

I know that i should have track of my object's counters but i am at a stage where my code is too much complexed and i need help..

Please help..

ALso tell that how much memory leak is allowed by apple?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南街九尾狐 2024-11-09 13:22:42

无法检查您是否“应该”释放对象。执行诸如“if(object)”之类的操作仅检查指向对象的指针。即使它指向的对象很久以前就被销毁了,它也会返回 true。这就是你的第二种情况发生的情况。当你调用release时,对象被销毁,但指针仍然指向某个东西,所以它返回true。仅当指针设置为 nil 时才会返回 false。

然而,有一套简单的调用释放的规则。如果您曾经对对象调用“alloc”、“new”、“copy”、“mutableCopy”或“retain”,则必须始终对其调用“release”或“autorelease”。这将防止任何内存泄漏。

Apple 没有公开允许的内存泄漏量。消除任何已知的内存泄漏始终是最安全的;此外,这还意味着您的客户可以获得更好的性能。

There is no way to check if you "should" release an object. Doing something like "if(object)" only checks the pointer to the object. It will return true even if the object it was pointing to was destroyed a long time ago. This is what happens in your second case. The object is destroyed when you call release, but the pointer is still pointing at something, so it returns true. It will only return false if the pointer is set to nil.

However, there is a simple set of rules for calling release. If you ever call "alloc", "new", "copy", "mutableCopy" or "retain" on object, you must always call "release" or "autorelease" on it. That will prevent any memory leaks.

Apple does not have a publicized amount of memory leaks allowed. It is always safest to eliminate any known memory leaks; plus, it will mean better performance for your customers.

全部不再 2024-11-09 13:22:42

在第二种情况下,您正在释放 NSMutableArray,但它仍然存储一个非零值,尽管它不再使用(调用函数或获取值)。这就是您的 if 条件为true

请记住,每当您对任何对象调用 release 时,不要忘记为其分配 nil ,因此您的第二个代码应如下所示。

CASE-2

NSMutableArray  *array=[[NSMutableArray alloc]init];
    [array release];
     array = nil;
    if(array)
    {
        NSLog(@"hiiiiiii"); 
    }

在 Object-C 中有一个简单的内存管理规则,如果您分配保留任何对象,您必须对其调用释放,

阅读 Apple 的内存管理指南。

In your second case you are releasing the NSMutableArray but still it store a non zero value although it's no longer for use (To call function OR fetch value).That the reason your if condition got true.

Just remember whenever you call release on any object, Do'nt forget to assign nil to that, So your second code should look like below.

CASE-2

NSMutableArray  *array=[[NSMutableArray alloc]init];
    [array release];
     array = nil;
    if(array)
    {
        NSLog(@"hiiiiiii"); 
    }

There is a simple rule of memory management in Object-C if your alloced or retain any object you must call release on that,

Read memory management Guide from Apple.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文