Objective-C 2.0的内存管理 [英] Memory management of Objective-C 2.0

查看:106
本文介绍了Objective-C 2.0的内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Objective-C的初学者。
我通过编写简单的命令行代码来学习Objective-C中的内存管理。



我的环境如下。




  • Mac OS X山狮。

  • Xcode4.5



我在下面写了一个代码。



test.m

  1 #import< Foundation / Foundation.h> 
2 #import< stdio.h>
3
4 @interface A:NSObject
5 - (void)myprint;
6 @end
7
8 @implementation A
9 - (void)dealloc {
10 printf(dealloc !! \\\
);
11 [super dealloc];
12}
13
14 - (void)myprint {
15 printf(myprint !! \\\
);
16}
17 @end
18
19 int main(void){
20
21 id obj1 = [[[NSObject alloc] init] autorelease];
22 id obj2 = [[A alloc] init];
23
24 [obj2 release];
25 [obj2 myprint];
26
27 return 0;
28}

我用下面的命令构建了这个代码

  clang -g -Wall -o main test.m -fno-objc-arc -framework Foundation 

生成成功,没有任何警告消息。
结果如下。

  dealloc! 
myprint !!

我有两个关于此结果的问题。



<第一个问题是关于autorelease方法。
我认为这个代码将引发运行时错误,因为
autorelease方法被调用没有NSAutoreleasePool实例。
为什么这个代码不会引起运行时错误?



第二个问题是关于dealloc方法。
obj2在调用dealloc方法后响应myprint方法。
为什么obj2在调用dealloc方法后回应myprint方法?



谢谢。

解决方案


第一个问题是关于autorelease方法。我认为这个代码将
引发运行时错误,因为autorelease方法被调用没有
NSAutoreleasePool实例。为什么这段代码不会引发运行时
错误?


没有自动释放池,因此,有点意外,您在运行时没有看到没有池位警告。这不会是一个错误,但只是一个运行时警告,发生泄漏。



奇怪。我看到同样的行为。我会问。


第二个问题是关于dealloc方法。 obj2在调用dealloc方法之后响应myprint
方法。为什么在调用dealloc方法后obj2响应
myprint方法?


未定义的行为。 obj2 已被解除分配,但解除分配并不意味着内存已清除。



Malloc Scribble(在解除分配时在记忆上涂鸦),你会看到预期的崩溃:

  env MallocScribble = 1 ./main 
dealloc!
分段错误:11

更好的是,如果打开僵尸检测:

  env NSZombieEnabled = YES ./main 
dealloc!
2012-12-06 08:10:14.580 main [80114:f07] *** - [A myprint]:消息发送到释放实例0x7f9b7ac09dd0
跟踪/ BPT陷阱:5


I'm a beginner in Objective-C. I'm studying memory management in Objective-C by writing simple command line code.

My environment is below.

  • Mac OS X Mountain Lion.
  • Xcode4.5

I wrote a code below.

test.m

1     #import <Foundation/Foundation.h>
2     #import <stdio.h>
3     
4     @interface A : NSObject
5     -(void)myprint;
6     @end
7     
8     @implementation A
9     -(void)dealloc {
10       printf("dealloc!!\n");
11       [super dealloc];
12    }
13    
14    -(void)myprint {
15       printf("myprint!!\n");
16    }
17    @end
18    
19    int main(void) {
20    
21       id obj1 = [[[NSObject alloc] init] autorelease];
22       id obj2 = [[A alloc] init];
23    
24       [obj2 release];
25       [obj2 myprint];
26       
27       return 0;
28    }

I built this code with below command.(Build with No ARC option)

clang -g -Wall -o main test.m -fno-objc-arc -framework Foundation

The build succeeded with no any warning message. The result was below.

dealloc!!
myprint!!

I have two questions about this result.

First question is about autorelease method. I think this code will raise a runtime error because autorelease method is called with no NSAutoreleasePool instance. why doesn't this code raise a runtime error?

Second question is about dealloc method. obj2 responds to myprint method after dealloc method was called. Why does obj2 respond to myprint method after dealloc method was called?

Thanks.

解决方案

First question is about autorelease method. I think this code will raise a runtime error because autorelease method is called with no NSAutoreleasePool instance. why doesn't this code raise a runtime error?

There is no autorelease pool and, thus, I'm a little surprised you don't see the no pool in place warning when run. It wouldn't be an error, though, just a runtime warning that a leak occurred.

Odd. I'm seeing the same behavior. I'll ask.

Second question is about dealloc method. obj2 responds to myprint method after dealloc method was called. Why does obj2 respond to myprint method after dealloc method was called?

Undefined behavior. obj2 has been deallocated, but deallocation doesn't mean that the memory has been cleared.

If you turn on Malloc Scribble (which scribbles on memory on deallocation), you'll see an expected crash:

env MallocScribble=1 ./main
dealloc!!
Segmentation fault: 11

Better yet, if you turn on zombie detection:

env NSZombieEnabled=YES ./main
dealloc!!
2012-12-06 08:10:14.580 main[80114:f07] *** -[A myprint]: message sent to deallocated instance 0x7f9b7ac09dd0
Trace/BPT trap: 5

这篇关于Objective-C 2.0的内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆