Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Release an object without a pointer?

I’ve just started developing for iPhone and am trying to get my head around memory management. I made a small program that shows a map and an annotation on the map. For the annotation I made a simple class that implements the MKAnnotation protocol.

To create and add the annotation I wrote this:

[self.myMapView addAnnotation:[[MyAnnotation alloc] init]];

It worked fine until I tried to release the object. Nothing to release. This is what I would have done in C#, I guess it doesn’t work without garbage collection?

So is this the only way to do it?

MyAnnotation *myAnnotation = [[MyAnnotation alloc] init];

[self.myMapView addAnnotation: myAnnotation];

[myAnnotation release];
like image 256
Kai Friis Avatar asked Jul 12 '26 05:07

Kai Friis


1 Answers

[self.myMapView addAnnotation:[[[MyAnnotation alloc] init] autorelease]];
like image 200
codelark Avatar answered Jul 14 '26 20:07

codelark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!