Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 06-25-2009, 11:14 AM   #6 (permalink)
smasher
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,577
Default

Here's one problem:

Code:
	NSString *num = [NSString alloc];
	num = [formatter stringFromNumber:n];
You alloc some memory for a string, and you never use that memory or release it. It's probably getting leaked.

Your code basically says "reserve some memory for a string, and point the pointer 'num' at it." And then "point the pointer 'num' at a different string instead.' You don't need to alloc any memory if you're just going to point the pointer at a different object. This code would be just fine:

Code:
	NSString *num;
	num = [formatter stringFromNumber:n];
Also, it's more typical to release objects right after you add them to an array - that way when array gets deallocated, all the objects inside get deallocated too (because they only had a retaincount of one.)

I can see from your code that the objects in your array get a retaincount of two - one from when they were created, and one from adding to the array. That's why you find yourself having to release them after you remove them.

If you've added them to the array and then released them, then a simple [entities removeObject:theEntity] would cause the entity's retaincount to go to zero, and it would be destroyed. Futhermore, assuming the array had a retaincount of one, [entities release] would send a release to every object in the array, causing their counts to go to zero, causing them to be destroyed.

If what I'm saying seems confusing, then you should read up on memory management:
http://www.stepwise.com/Articles/Tec...-03-11.01.html
__________________

Last edited by smasher; 06-25-2009 at 11:17 AM.
smasher is offline   Reply With Quote
 
Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,357
Threads: 39,153
Posts: 171,637
Top Poster: smasher (2,577)
Welcome to our newest member, davidm
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:14 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.