Question about the lifetime of a factory object in Objective-C / Cocoa (save or not ...)

From reading the memory management documents in the SDK, I realized that factory methods (static constructor methods) usually save the object for me and add it to the autocomplete pool?

Would that mean I don't have to save and release the object until the pool is released before I expect it? (What should be at the end of the application, for the default autostart pool in main ()?)

This question is: Cocoa Touch a question. Should [NSMutableArray array] be saved? seems to agree with that.

However, when I used the NSMutableArray arrayWithCapacity: method, I found that I needed to save the array or all my cheeks were torn.

I'm sure this is just a complete n00b-ness or a strange mistake elsewhere in the code, but if someone could explain how exactly I might have misunderstood, I would be very obliged.

Thank!

Update: thanks for the answers. I think I have it now!

+3
source share
4 answers

From reading the memory management documents in the SDK, I realized that factory methods (static constructor methods) usually save the object for me and add it to the autocomplete pool?

Yes, you need not worry about the release of the objects returned from any method other than the methods alloc, init, new, and copy.

, , , ?

.

( , main()?)

. , - , . , , .

, , , "". "" , .

, , NSMutableArray , retain . , [[NSMutableArray alloc] initWithCapacity: ];

.

+8

. , , , , , .

, NSMutableArray , , , . , dealloc.

+4

, . , , , , . , , Event Loop, .

. :

while(wait for event) {
    1) create autorelease pool
    2) handle event
    3) release pool
}

, , - , , . , , . , .

iPhone http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Art/app_life_cycle.jpg

+1

As soon as you add your object to the array, there is an implicit call to save for that object, which means that after adding to the array you can free it from the source, and when you call the delete method from the array it will also have an implicit call for release.

0
source

Source: https://habr.com/ru/post/1727567/


All Articles