Help me understand memory management in Objective-C and Cocoa

Don't shoot me, I know it must have been asked a thousand times ...

I don't like the lack of good documentation in Objective-C's memory. I understand alloc, dealloc, save, release and all this, but there is confusion in my head.

Is it just lazy programming or is Objective-C doing some backstage automatic activity regarding memory allocation?

Based on background C (centuries ago), I know that pointers are just pointers ... you also need to reserve a place to point to a pointer, or you will start stomping on your own program and other variables.

The code samples that I find (and which are in the books I read - all of which are sadly outdated with the current version of Xcode and Interface Builder) never allocate storage space for some objects, such as NSString. They declare a pointer (for example, NSString * aString;), and then begin to assign text to the line. No memory allocation for the string is called!?!? So, all these examples are just lazy code looking for a place to crash?

In addition, books talk about declaring pool memory and that it is automatically inserted into your code. When I create projects, classes and objects, such code is not found anywhere. Is Apple done with this auto-insertion, or is it something that happens at compile time?

Is there a penultimate guide or website that will explain all this once and for all?

+3
5
+15

, , Objective-C, Objective-C 2.0, PDF Apple URL:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/ObjC.pdf

3 .

, Objective-C Cocoa, URL :

http://www.stepwise.com/Articles/Technical/2001-03-11.01.html

, NSString, .

", (, NSString stringWithString), ."

Cocoa , : Cocoa Mac OS X (3- ), Amazon :

http://www.amazon.com/exec/obidos/ASIN/0321503619/bignerdranch-20

Aaron Hillegass Objective-C Cocoa . , , . , .

, .

+2

, :

  • Factory , [NSString stringWIthString:theString] [[[NSString alloc] initWithString:theString] autorelease] (, , ). autorelease NSAutoReleasePool, , .
  • Objective-C . malloc new , Objective-C , NSObject ( Apple Objective-C Objective-C), +(id)alloc . , , , malloc .
0

Objective-C 2.0 .

NSString * aString; . , - :

NSString * aString = [[NSString alloc] init];

" C Mac?" , . XCode.

0

Of course, this question has been asked for a long time, and good answers have already been given here. But now at least one of them is outdated - the one regarding the garbage collector

And if, maybe, someone who comes here and is looking for a book, here is one: https://www.packtpub.com/application-development/objective-c-memory-management-essentials

I am a co-author, yes, why I could not pass by ... :) Any critics are welcome!

0
source

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


All Articles