IPhone memoryThe management summary is the content to be introduced in this article,IPhoneIn Objective-C development, as long as some Apple programming rules are followedMemoryManagement is easier, but there are also a lot of things to be aware.MemoryDebugging is also a headache.
1. For example, if an EXC_BAD_ACCESS error occurs, the error message is displayed to you. There is still a way to know how to identify the error,
Make the following settings:
- Project -> Edit active executable ->Argument
Add the following four parameters:
- NSDebugEnabled
- NSZombieEnabled
- MallocStackLogging
- MallocStackLoggingNoCompact
And set them to YES. For example:
At this time, if you have the following code:
- // Release a variable again
- NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init];
- NSData * data = [NSData dataWithBytes: "asklaskdxjgr" length: 12];
- [Data release];
- [Pool release];
The following prompt is displayed in the Debug window:
- 2003-03-18 13:01:38.644 autoreleasebug[3939] *** *** Selector 'release'
- ent to dealloced instance 0xa4e10 of class NSConcreteData.
Although it is possible to roughly determine which type of variable is released repeatedly, there is not enough information. When the project is large and the source code is large, it is not easy to locate,
Run the following command in the console to obtain more information:
- shell malloc_history <pid> <address>"
Enter the following command:
- shell malloc_history 3939 0xa4e10
More information is displayed:
- [dave@host193 Frameworks]$ malloc_history 3939 0xa4e10
- Call [2] [arg=32]: thread_a0000dec |0x1000 | start | _start | main |
- +[NSData dataWithBytes:length:] | NSAllocateObject | object_getIndexedIvars |
- malloc_zone_calloc
At this time, we will know which function has a problem first. here we can see that the NSData in main has a problem.
2. NSArray and other collection classes.
The following code
- ReleaseTest* rt = [[alloc] init];
- NSMutableArray *array = [[NSMutableArray alloc] init] ;
- [array addObject: rt];
- ReleaseTest *rt2 = [array objectAtIndex:0];
- [rt2 release];
- [array release];
- [rt release];
Will lead to repeated releaseMemoryThe problem is that rt2 gets a pointer to an object. If it has been released, rt will be released again. In order to follow the principle of Init and Release, rt2 should not be Release.
3. Problems with init and Release.
All objects generated by using the Init method must be Release by themselves.
Objects not generated using the Init method do not need to be responsible for Release. for example, if the [NSString StringWithFormat] method generates an object, you do not need to perform the Release operation on your own. Therefore, you must define the function and set it to autoRelease when returning a class. In this way, the caller does not have to worry about whether to release the object.
4. In the AutoReleasePool, it is best not to assign the AutoRelease object to other objects. Otherwise, after leaving this scope, the object will be Release.
Summary: AboutIPhone memoryThe management summary is complete. I hope that this article will help you.IphoneIf you are interested in development, please go to the iphone development channel to learn more.