IOS memory management: IOS has no garbage collection mechanism (Mac OS) for different Mac OS and iOS, mainly because mobile devices have limited resources? Or is his exclusive framework cocoa not supported? Or the two are listed. Therefore, in IOS programming, memory management is very important-reference the counter (RC). If you do not pay attention to it, there may be a memory leak or bad access (access to a destroyed object address) error.
- The basic principle of Memory Management: You can only cancel (release) or release (autorelease) the objects you own, according to the naming rules of cocoa, you generally have the ownership of a when creating an object A by using the method named alloc, copy *, New * (xxx * Indicates containing XXX. Or use retain to obtain the ownership of an object.
- Use release and autorelease to release the ownership of an object. Release means immediate release, and the Object Reference Counter-= 1 (note that using the object xxxcount to view the object reference count may cause misunderstanding. According to XXX, when the reference count of an object is = 0, the delloc object is called and the object is destroyed, however, when you call xxxcount of an object to view the number of object references, it is usually greater than your expected value 0. You do not have to worry about this because you do not know the relationship between classes in this framework, you do not need to know. All you need to do is ensure that get (get ownership) in your code)
--> Release/autorelease ownership. In iOS, you are just a passer-by, not a master. You have to check out your reservation (not accurate enough? Indeed, the reservation has no ownership.
- The second point above only refers to release. Here we will talk about autorelease, auto... (autorelease, the same as below) means 'release will send release message'. In fact, it is to put the object into the corresponding Auto Release pool (proximity principle ?), When will it be released? This is related to autoreleasepool (automatic release pool). Before the release/drain pool, a release message is sent to each object in the pool. The automatic release pool has its own scope. Well, let's talk about the places where autorelease is used most...
-(Void) creatobject {nsstring * s = [[nsstring alloc] init] // create a String object s return [s autorelease] // You must develop a good habit, today, when you create an object in the // return method (please --), put it into the Auto Release pool before returning it}
- Autoreleasepool. Cocoa applications always expect an available Auto Release pool. If the Auto Release pool is unavailable or does not exist, the automatically released object cannot be released, memory leakage (leak ).
- The automatic release pool can be nested. What is nesting? Check the Code:
-(Void) run {ngutoreleasepool * Pool = [[ngutoreleasepool alloc] init] // pseudo code, meaning meeting... // some code here nyothoreleasepool * innerpool = [[nyothoreleasepool alloc] init] // nested automatic release pool... // some code here [innerpool release] [pool release]}
In fact, in the stack where the cocoa application runs, but you create an automatic release pool, it will be added to the top of the stack (along with the method), so to prevent memory leakage, there should also be an automatic release pool at the bottom of the stack. No. There is no such thing in the IOS main method .... The scope of the automatic release pool is
Init-> release (in the same Upper and Lower environments, you can simply think of it in the same method), nested automatic release pool, usually used to create a large number of temporary objects.
-(void)run{ for (int i= 0,i <99999,i++){ NSAutoReleasePool *innerpool= [[NSAutoReleasePool alloc] init] NSString *s = [[NSString alloc] init] ... //some code here [s autorelease] [innerpool release]}}
- Thread and pool. Each thread has its own thread stack.
- Exception and pool. parse the fourth red font.
- On the question of 'chicken eggs, egg bean', you just need to confirm that the chicken can have eggs.
- (To be continued)