If I create a UILabel programmatically, is it necessary to release it afterwards? E.g.,
UILabel *label = [[UILabel alloc] initWithFrame:...];
[self.view addSubView:label];
[label release]; // <-- is this needed?
I found this thread that included the release line, but I understand that this works differently in the newer iOS versions.
Thanks for reading.
Yes you need to release it unless you are using ARC.
In this case when you
allocyour retain count is 1.When you do
addSubViewthe view willretainyour label : Retain count is 2.So at that point you are free to make the
releaseto balance out yourallocretain count is now 1.But that 1 is
ownbyself.viewand it will call release when it doesn’t need it anymore.When that happen the retain count of your label will drop to 0 and it will be
deallocatedand it’s memory will be free.