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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 06-01-2011, 08:15 AM   #1 (permalink)
Registered Member
 
Join Date: Oct 2009
Location: Manchester, UK
Posts: 23
Obelisk is on a distinguished road
Default Disposing of a class correctly?

Hi All!

I've created a basic NSObject class that defines a particle effect. The class contains some basic float values, and a UIImageView object that represents the particle graphic itself.

When, in the main program, I want to create a new particle effect, I would do so thusly:

Particle *p = [[Particle alloc] init];

Then, set the image inside the class:

p.imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"particle-red.png"]];

Then, add the image to the current view:

[self.view addSubView: p.imgView];

I'm having some trouble working out how to correctly dispose of the class once I'm finished with the particle? Inside the particle class, there's some animation stuff done, after which time it runs some code like this:

self.Dead = YES;
[self.imgView removeFromSuperView];

Which all works great. However, watching the memory profiler - I can see that the memory for the image is disposed, but there's still a single allocation remaining (presumably for the class the ImageView belongs to), and I'm not sure how to remove it?

I was toying with the idea of just pushing new Particles instances into an NSMutableArray which gets crawled every few cycles, and any particles with the 'Dead = YES' flag set are popped from the array.

Any thoughts on this would be most welcome.

Thanks!
Obelisk is offline   Reply With Quote
Old 06-01-2011, 08:23 AM   #2 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

the problem is that you alloc w\o release

Code:
p.imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"particle-red.png"]];
[self.view addSubView: p.imgView];
[p.imgView release];
however your code seem that is not really mvc compliant...
__________________
dany_dev is offline   Reply With Quote
Old 06-01-2011, 08:28 AM   #3 (permalink)
Registered Member
 
Join Date: Oct 2009
Location: Manchester, UK
Posts: 23
Obelisk is on a distinguished road
Default

Quote:
Originally Posted by dany_dev View Post
the problem is that you alloc w\o release

Code:
p.imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"particle-red.png"]];
[self.view addSubView: p.imgView];
[p.imgView release];
however your code seem that is not really mvc compliant...
The release for the image view is handled in the dealloc of the Particle class to which it belongs:

- (void)dealloc
{
[imgView release];
[super dealloc];
}

Why would you say my code is not very MVC compliant? Adding views as subviews of another view is quite a normal thing to do, and there's no memory leaks etc. with any code I've done in this manner.
Obelisk is offline   Reply With Quote
Old 06-01-2011, 08:38 AM   #4 (permalink)
Registered Member
 
Join Date: May 2010
Location: London
Posts: 90
Renaissance77 is on a distinguished road
Default

Quote:
Originally Posted by Obelisk View Post
Why would you say my code is not very MVC compliant?
You said that it's a "basic NSObject class that defines a particle effect" - from your description, that sounds like a model class.

In proper MVC style, a model class should not be referencing/retaining a view.
Renaissance77 is offline   Reply With Quote
Old 06-01-2011, 08:46 AM   #5 (permalink)
Registered Member
 
Join Date: May 2010
Location: London
Posts: 90
Renaissance77 is on a distinguished road
Default

Quote:
The release for the image view is handled in the dealloc of the Particle class to which it belongs:
Assuming "imgView" is a property with the "retain" attribute, you are retaining it twice. The "alloc" retains it, and setting "p.imgView" retains it.

So you need both the release (or autorelease) call when you alloc the UIImageView, and the release call in dealloc.

Last edited by Renaissance77; 06-01-2011 at 08:59 AM.
Renaissance77 is offline   Reply With Quote
Old 06-01-2011, 08:50 AM   #6 (permalink)
Registered Member
 
Join Date: Oct 2009
Location: Manchester, UK
Posts: 23
Obelisk is on a distinguished road
Default

Quote:
Originally Posted by Renaissance77 View Post
You said that it's a "basic NSObject class that defines a particle effect" - from your description, that sounds like a model class.

In proper MVC style, a model class should not be referencing/retaining a view.
Ok, fair comment. I just didn't want the overhead of using a UIViewController object as a single particle. Having an NSObject class that contains just a UIImageView object seemed like a cleaner solution. I've found a way around this, in any case.

When I create a particle, I push the Particle class into a mutable array. Then, in the parent class I have a timed function that periodically checks the array for dead particles and removes them - which immediately deallocs the class and releases the retained view inside.

Seems like a fairly simple setup, and has removed the surplus object allocation problem I had.

Thanks for your input in any case, much appreciated.
Obelisk is offline   Reply With Quote
Old 06-01-2011, 08:55 AM   #7 (permalink)
Registered Member
 
Join Date: Oct 2009
Location: Manchester, UK
Posts: 23
Obelisk is on a distinguished road
Default

Quote:
Originally Posted by Renaissance77 View Post
Assuming "imgView" is a property with the "retain" attribute, you are retaining it twice. The "alloc" retains it, and setting "p.imgView" retains it.

So you need both the release (or autorelease) call when you assign p.imgView, and the release call in dealloc.
Sorry, I should have been more clear. When I declare the particle, I release it immediately after calling [self.view addSubView.imgView], otherwise I'd have a surplus allocation. Don't worry, I've got my head screwed on with that stuff.

My problem was that there was the object attached to the main view with seemingly no way to release the parent class (thus releasing the image view). I've since plugged the Particle class instances into an NSMutableArray - giving me much more control over releasing them individually.

Just run this through the profiler and all object allocations and memory is cleaned up perfectly once a particle has finished its stuff on screen.
Obelisk is offline   Reply With Quote
Old 06-01-2011, 09:18 AM   #8 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Actually, you'd have an extra retain, not allocation. If you need to keep reference to those particle views you can, as you did, either use an array, but you can also iterate through the subviews (which is a property of uiview) of the view to which you are adding them. There's also the viewWithTag option.
baja_yu is offline   Reply With Quote
Reply

Bookmarks

Tags
memory, particle, particles, uiimageview

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: 339
5 members and 334 guests
Dnnake, iOS.Lover, jenniead38, Kirkout, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

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