Three Principles of Memory Management in OC:
1. Who created (new or alloc) and who release.
2. If you get an object from a method, assume that the retaincount of this object is 1 and has been set to autorelease. You don't need it, and you shouldn't release it. If you need to pass this object further, you can perform retain or copy operations.
3. If you retain an object, you must be responsible for release it.
In addition. Carefully read the definition of the release method, which is defined as oneway. This keyword means that the method call does not need to return any messages, and the return of the method does not guarantee that the method has been completed. That is to say, when you call [OBJ release], retaincount does not necessarily respond immediately. Therefore, retaincount is unreliable unless you are willing to sleep for a while to wait for the release to complete.
Another point is that if the attributes of a class are defined by @ property (retain), it is best to do two things at the same time when releasing, 1, release it, 2, set it to nil through setter. To avoid confusion caused by pointer pointing to random location of memory.
The third point to be supplemented is that when a created object is returned using a factory-like method, the object must be returned in the following way:
Return [product autorelease];