i have got the following problem: with a timer i call the function draw repeatly (in the timer runLoop function) and found out, that the following commands will increase the memory usage permanently:
a CGRect is only a structure, right? why is this allocating memory constantly?
i also tried to add a NSAutoReleasePool to the timer function, but that is also not working. furthermore, i tried to alloc the NSString and release it again at the end of the function, but that didn't help.
what makes you so certain that these pieces of code are the cause of your ever-increasing memory usage?
i reduced the program to the very basic and then i did many tests by commenting various commands. And when i commented those, the memory usage didn't increase.
game will alloc the hud class (inherited from base class) and include it into its entities. then, when game is started, it runs the timer, which will call the draw method every 1/100 s.
in the hud class draw method, there are the previously mentioned commands (except for the CGRectMake, which is in the super draw method).
Code:
- (void)draw
{
NSNumber *n = [NSNumber numberWithFloat:self.number];
NSString *num = [NSString alloc];
num = [formatter stringFromNumber:n];
// the rest is unimportant
[super draw];
[num release];
}
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.
thanks for your replay, the link and your explanation! i already thought that it has to be that way, but i couldnt figure out, how to get a fast access to the entity in the array. is there any possibility to create a key or something for an array item? well...i could add something like an entiyName property to each object and go through all entities to find it, but is there any easier method for that?
well...i already tried to not allocate the NSString (exactly the way you suggested), but that did not work either. also the NSNumber should be autoreleased, as far as i understood that. and what about the CGRectMake?
what could that be?
or am i missinterpreting the instruments? everything is increasing except of net bytes. is this the important number or all other things (#net, overall bytes, #overall)?
thanks for your replay, the link and your explanation! i already thought that it has to be that way, but i couldnt figure out, how to get a fast access to the entity in the array. is there any possibility to create a key or something for an array item? well...i could add something like an entiyName property to each object and go through all entities to find it, but is there any easier method for that?
I don't understand the connection between my explaination and "fast access to the entity in the array" , so we might be misunderstanding each other - however you can get objects directly from the array using objectAtIndex: . If you want to store objects by name, you could use a dictionary instead, with the name as the key. Dictionaries do not keep items in order, though.
Quote:
or am i missinterpreting the instruments? everything is increasing except of net bytes. is this the important number or all other things (#net, overall bytes, #overall)?
You may be fine! Overall Bytes and Overall # are the total number (and size) of objects that were allocated EVER, even if you released them properly. Those will always go up.
The Net # and Net bytes are the amount you're using now - they're also represented in the blue graph at the top of instruments. They will go up and down a little. If they climb like a mountainside and wind up over 8-12MB, you're in trouble.
i ment that I add e.g. the hud object to the object game, and add it then to the entities array to automatically call the function draw of hud.
but in the games draw method i also have to access the hud object to change some properties. when i release it, i can't simply write hud.speed e.g.
well, i will try the NSDictionary approach, thanks for the advice!
ok, the net bytes number isn't increasing and the diagram is always blue and not getting bigger. do i need to take care of the # net? because this is also increasing permanently.