How do I implemet release method?
-
22-04-2021 - |
Question
Objective c has release method. I want to create my own method that duplicate objective c method. But I don't know hot to free a memory.
-(void) release
{
if(retainCount == 1)
{
[self freeMemory];
}
else
{
retainCount --;
}
}
-(void) freeMemory
{
}
Solution
The short answer is that you would call [self dealloc], but you really shouldn't be doing stuff like this, ever ever ever. Under ARC it's not even possible to write this kind of code.
Your method exactly duplicates the functionality of the release method that your class will already have inherited from NSObject, so it's very difficult to see what the point is.
If you want to do something extra when release is called, why not just call [super release]?
Can you explain what it is you want to do at a higher level - maybe there's an alternative approach?
OTHER TIPS
In your freeMemory
function you should release all retained class members and that's it.