简体   繁体   English

iOS目标C中的内存管理

[英]memory management in ios objective c

I am new to iOS and objective-C and although I've been struggling for a while to understand memory management I am disappointed in myself because I still have to get the full picture... My problem is that I do not understand how retaining a property of an object relates with retaining the whole object. 我是iOS和Objective-C的新手,尽管我花了一段时间努力了解内存管理,但我对自己感到失望,因为我仍然必须全面了解...我的问题是我不了解如何保留对象的属性与保留整个对象有关。 Let's take the following code as an example: 让我们以以下代码为例:

@interface TestObject:NSObject {   //TestObject declaration
  NSNumber *firstNumber;
}

@property (nonatomic, retain) NSNumber *firstNumber;

@end

@synthesize firstNumber;

-(void) dealloc  //Use standard synthesized getter and setter, write only custom
                 //dealloc                  
{
[firstNumber release];
}

...and the following code that uses it: ...以及使用它的以下代码:

-(IBAction) runClicked: (id) sender  
{
TestObject *to1=[[TestObject alloc ] init];
to1.firstNumber=[NSNumber numberWithInt:10];  //retain count 1 on firstnumber
NSNumber *num=[to1.firstNumber retain]; //retain count 2 on firstnumber
[to1 release]; //retain count 1 on firstnumber because of 1 release in dealloc
}

I ran an analyze on the code and also ran the program with Leak instrument and no leaks were found by either. 我对代码进行了分析,并使用Leak仪器运行了程序,两者均未发现泄漏。 Isn't there a leak on firstnumber (accessible by num after main object release) since the number will not be usable by any pointer after *num is also destroyed at the end of function body? * number在函数体末尾也被销毁之后,firstnumber(在释放主对象后可由num访问)是否没有泄漏,因为该数字将不能由任何指针使用?

Thank you so much for your time! 非常感谢您的参与! Best regards, Florin. 最好的问候,弗洛林。

不,没有泄漏,因为第一个数字是自动释放的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM