iphone - iOS addSubview & removeFromSuperview causing memory leaks -
i have following issue:
i have 1 instance class extends uiviewcontroller class, in class adding uiimage uiimageview, , adding uiimageview self.view using method "addsubview:" [self.view addsubview:imageview] , adding self.view window using [[[uiapplication sharedapplication] keywindow] addsubview:[self view]] , , when click on close button clearing self.view (removing subview "uiimageview") , removing self.view window.
now issue when using same instance add other uiimageview other image, first data not freed memory have released them , removed them superview. following code illustrates problem.
- (void) setimagewithurl: (nsstring *)url { nsdata * imagedata =nsdata * imagedata =[nsdata datawithcontentsofurl:[nsurl urlwithstring:url]]; if (imagedata) { uiimage *image = [[uiimage alloc]initwithdata:imagedata]; [[[uiapplication sharedapplication] keywindow] addsubview:[self view]]; [self createviewwithimage:image]; [image release]; } -(void) createviewwithimage: (uiimage *) image{ [self.view sethidden:false]; uiimageview *imageview = [[uiimageview alloc] initwithimage:image]; [self.view addsubview:imageview]; [imageview release]; } // removing imageview when click on close button -(void) closeview{ (uiview *subview in self.view.subviews) { [subview removefromsuperview]; } [self.view removefromsuperview]; }
note: have used xcode instruments test memory allocation.
update: after more research .. not addsubview & removefromsuperview make memory leaks.. nsdata. dont why not released memory. have made changes test if nsdata, saved image locally , set image using 2 cases:
//case 1: nsstring *path = [[nsbundle mainbundle] pathforresource:@"image" oftype:@"png"]; nsdata * imagedata =[[nsdata datawithcontentsoffile:path]; image = [[uiimage alloc]initwithdata:imagedata]; //case 2: nsstring *path = [[nsbundle mainbundle] pathforresource:@"image" oftype:@"png"]; image = [[uiimage alloc] initwithcontentsoffile:path];
i have found case 1 makes memory leaks, while case 2 clean.
now uiimage cant take image without using nsdata, , reason nsdata not released .. how can fix problem !!!!
do have a:
[super dealloc];
in deriving class's dealloc?
Comments
Post a Comment