Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-18-2010, 01:03 AM   #1 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 11
Default Can someone please confirm this code? ([object release] issue)

I have a small method which is called to show a small picture on the screen for a short period of time. This method is called lots of times during the life of the application.

In summary, I dynamically allocate the UIImageView object and then release it. Will the following code give me a memory leak? is this correct way of doing such an action? any better way?

Thank you so much in advance!


- (void)showPicture {

UIImageView *pic = [[UIImageView alloc] init];

pic.image = [UIImage imageNamed:@"aPicture.png"];
pic.frame = CGRectMake(20, 20, 20, 20);
pic.alpha = 1.0;
[self.view addSubviewic];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
pic.alpha = 0.0;
[UIView commitAnimations];

[pic release];

}
soonio is offline   Reply With Quote
Old 03-18-2010, 01:30 AM   #2 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 10
Default

Quote:
Originally Posted by soonio View Post
I have a small method which is called to show a small picture on the screen for a short period of time. This method is called lots of times during the life of the application.

In summary, I dynamically allocate the UIImageView object and then release it. Will the following code give me a memory leak? is this correct way of doing such an action? any better way?

Thank you so much in advance!


- (void)showPicture {

UIImageView *pic = [[UIImageView alloc] init];

pic.image = [UIImage imageNamed:@"aPicture.png"];
pic.frame = CGRectMake(20, 20, 20, 20);
pic.alpha = 1.0;
[self.view addSubviewic];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
pic.alpha = 0.0;
[UIView commitAnimations];

[pic release];

}
Don't see any leaks (as long as you make sure to remove the "pic" view later instead of just creating a new UIImage view on top of it. (Also if you want to check code for leaks, the Leaks tool included in the XCode env is great for exactly this sort of thing, and is defiantly something to learn how to use).
My question would be though, if you use this view a lot and are constantly calling this code, why not just keep the UIImageView around and simply replace the image it contains? Constantly creating a new UIImageView seems like the roundabout way of replacing an old image. Save yourself (and the device) some work and just do it once.
AlexTheMighty is offline   Reply With Quote
Old 03-18-2010, 01:31 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 63
Default

I think there is no memory leak here. You can run performance tool Leak to confirm that
firearasi is offline   Reply With Quote
Old 03-18-2010, 01:44 AM   #4 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 11
Default

Quote:
Originally Posted by AlexTheMighty View Post
Don't see any leaks (as long as you make sure to remove the "pic" view later instead of just creating a new UIImage view on top of it. (Also if you want to check code for leaks, the Leaks tool included in the XCode env is great for exactly this sort of thing, and is defiantly something to learn how to use).
My question would be though, if you use this view a lot and are constantly calling this code, why not just keep the UIImageView around and simply replace the image it contains? Constantly creating a new UIImageView seems like the roundabout way of replacing an old image. Save yourself (and the device) some work and just do it once.
Thank you for your quick reply! The reason I don't replace the image is that I want the picture to remain visible for the duration of animation... when I replace it, then the old picture disappears... if you understand what I mean. The actual code I use (which I didn't mention in the thread to make things simple) is:

- (void)showTouchedPoint_XCoordfloat)XCoord YCoordfloat)YCoord {

UIImageView *touchedPointImageView = [[UIImageView alloc] init];

touchedPointImageView.image = [UIImage imageNamed:@"touchedPoint.png"];
touchedPointImageView.frame = CGRectMake(XCoord, YCoord, 20, 20);
touchedPointImageView.alpha = 0.8;
[self.view addSubview:touchedPointImageView];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
touchedPointImageView.alpha = 0.0;
[UIView commitAnimations];

[touchedPointImageView release];

}

Can you please have a look at it again and tell me if this is ok? Also when I release the imageview, then the allocated uiimageview object is freed and so is the memory for that object, right? (sorry for the noobiness of the question.)
soonio is offline   Reply With Quote
Old 03-18-2010, 04:41 AM   #5 (permalink)
iOS / MacOS Developer
 
bharath2020's Avatar
 
Join Date: Oct 2008
Location: Bengaluru
Posts: 150
Default

Quote:
[self.view addSubview:touchedPointImageView];
Are you sure you are removing touchedPointImageView from its superview after animation?

for eg:

Code:
[touchedPointImageView removeFromSuperview];
Although there is no leak in your code snippet, if you do not remove the imageview from superview, you would end up creating a pool of imageviews which is released only when your self.view releases.


The better approach to this problem is to create the imageview once, and then play with its alpha and hidden property to hide and show the imageview.

Hope this Helps
__________________
Bharath
iOS Blog : http://iphone2020.wordpress.com
bharath2020 is offline   Reply With Quote
Old 03-18-2010, 05:11 AM   #6 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 11
Default

bharath2020, thank you for that. As I said, if I just use one imageview, I can't get the effect I need. With just one imageview, the moment the method is called for the second time, the picture which was initially shown disappears. I need the picture to remain for the duration of animation.

Is there a good way to remove the imageview from superview after say a period of time?
soonio is offline   Reply With Quote
Old 03-18-2010, 05:19 AM   #7 (permalink)
iOS / MacOS Developer
 
bharath2020's Avatar
 
Join Date: Oct 2008
Location: Bengaluru
Posts: 150
Default

Here is the appraoch

//FadeOut

1. Unhide the view by setting its hidden property to NO
1. Set Alpha of the view to 1.0
2. BeginAnimation Block
3. Register For AnimationDidEnd selector
4. set Alpha of the view to 0.0
5. End/Commit animation block

//Animation did End selector
Hide the view by setting its hidden property to YES
set the alpha back to 1.0//At this time your view is hidden and hence you can restore alpha


//Reverse the values in above steps for Fade In Effect

Hope this Helps
__________________
Bharath
iOS Blog : http://iphone2020.wordpress.com
bharath2020 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 259
18 members and 241 guests
14DEV, @sandris, ADY, ArtieFufkin10, ckgni, Dani77, HemiMG, IphoneSdk, jakerocheleau, JasonR, MACralik, NSeven, prchn4christ, Rudy, silverwiz, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:54 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0