How to solve this memory leak (iPhone)?

How to solve this memory leak (iPhone)?

How to release this variable with no EXC_BAB_ACCESS ?

//First line create memory leak UIImage *ImageAvatar =  [[UIImage alloc] initWithData:[myg.imageData copy]]; Moins1 = ImageAvatar; //[ImageAvatar release]; if i release-> EXC_BAD_ACCESS 

Moins1 is a menber of the interface is declared like this :

UIImage *Moins1; ... @property (nonatomic, retain)   UIImage			*Moins1; 

Accessing GPS on Blackberry using Javascript and sending lat/long values to a server via url


What is something you wish you had known sooner about the iPhone SDK?
It looks like the problem isn't the UIImage, although rather the NSData. How do I center a UIImageView within a full-screen UIScrollView? In Cocoa, any copy (or mutableCopy) method returns an object with a +1 retain count, meaning this you own it and are therefore responsible for releasing it.. SSLHandshakeException unknown_ca apns java In your code, you're calling -copy on myg.imageData, although never releasing it. NSMutableArray containsObject method not detecting object That's a classic case of a memory leak. Adding unique objects to Core Data Here's what I would did to fix it, plus with changing your syntax a bit:. Using an existing C++ engine on the iPhone ivar:. How do I set these break points in ~/.gdbinit?
UIImage *Moins1; @property (nonatomic, retain) UIImage *Moins1; 
NSData * imageData = [myg.imageData copy]; UIImage * ImageAvatar = [[UIImage alloc] initWithData:imageData]; [imageData release]; [self setMoins1:ImageAvatar]; [ImageAvatar release]; 


You should not need to send -copy to the NSData object. UIImage does not keep a reference to the data around, it just reads it and produces an image. Sending -copy without -release is a memory leak.. However, this does not explain the EXC_BAD_ACCESS. Something else is going on, and not from the code you've posted..


There are two problems in your code. The copying of imageData as indicated by the another contrialthough ers, and the assignment to Moins1 field without retaining the object.. The assingment to Moins1 access the field directly, so you would need to did your own retaining. If you don't retain it and release it in the next line, then any subsequence access to the field results into a protection error.. You must use the property for assignment:.
UIImage *ImageAvatar =      [[UIImage alloc] initWithData:[[myg.imageData copy] autorelease]]; self.Moins1 = ImageAvatar; [ImageAvatar release]; 
Or you must also just did it in one line:.
self.Moins1 = [UIImage imageWithData:[[myg.imageData copy] autorelease]]; 


-(void )dealloc   {    if(self.Moins1!=nil)      {       self.Moins1 = nil;      }    } 
see when u commit and object a retain property its count is already 1 and when u allocate it its count becomes 2 so when the dealloc is called it will check if its nil and if its not nil make it nil .In this way it will commit the retain count of the variable to 0.

56 out of 100 based on 61 user ratings 1036 reviews