memory management issues, first you need to know a concept, The reference count. OBJECTIVE-C memory management is based on the reference count (Reference count). If you want to use an object and want to make sure that the object is not freed during use, you need to ensure that the reference count >0 is used, and the reference count-1 after use. When the reference count is ==0, the Destroy method is called.
Then it is important to know which operations will use reference technology + 1 and which will-1. OK, here is a list:
+1 operation
Alloc-Create object is call Alloc, allocate memory for object, object reference count plus one. 、
Copy-Copies an object, returns a new object, and a reference count plus one.
Retain-reference count plus one to get ownership of the object.
-1 operation
Release-the reference count is reduced by one and the ownership is released. If the reference count is reduced to zero, the object is freed.
Autorelease-Release at some time in the future.
Autorelease is a special way of actually handing out the release to a thing called an auto-release pool (autorelease) that calls the release method when the system feels it needs to be cleaned. Due to the system to deal with, so may release the moment is not so timely, so in some necessary time, we still need to manually go to release.
Also a place to note is the container. Nsarray,nsdictionary These classes, when the object is added, the object's reference is automatically counted +1, and when it destroys itself, the reference count of these objects is 1. The same is true of the subview of UIView.
Memory management, we need to follow some basic principles:
1. Ensure that only functions with Alloc,copy,retain will have the reference count of +1.
2. In the object's Dealloc function, dispose of the instance variables owned by the object.
3. Never call Dealloc directly to release an object, relying entirely on the reference count to complete the object's release.
4. In many class methods, you can create autorelease objects directly.
5. When a parameter is passed out, it is generally set to Autorelease object because it is to be released by others.
Basic knowledge of IOS memory management