I am leaking memory on this:
my custom class:
+ (id)vectorWithX:(float)dimx Y:(float)dimy{
return [[[Vector alloc] initVectorWithX:dimx Y:dimy] autorelease]; }
- (Vector*)add:(Vector*)q {
return [[[Vector vectorWithX:x+q.x Y:y+q.y] retain] autorelease]; }
in app delegate I initiate it:
Vector *v1 = [[Vector alloc] initVector];
Vector *v2 = [[Vector alloc] initVector];
Vector *vtotal = [[v1 add:v2] retain];
[v1 release];
[v2 release];
[vtotal release];
How this leaks? I release or autorelease them properly. The app crashes immediately if I don't retain these, because of an early release I guess. It also crashes if I add another rele开发者_开发百科ase.
Why do you think you're leaking memory? Start with that. And what object do you crash on accessing? That will tell you most likely what object your under-retain is related to. If I had to make a guess, I'd suspect initVector, just because it's a very strange name for a method. What does it do? Why is it not called just "init?"
Is this threaded code? You're doing a lot more retain/autorelease than is generally appropriate. You do not need to retain an object in order to hold onto it through the current event loop. Generally speaking you only retain ivars, because those are what you need through the next event loop. If you have a lot of calls to retain outside of accessors, then you're almost certainly mis-managing memory. The above should be:
+ (id)vectorWithX:(float)dimx y:(float)dimy
{
return [[[Vector alloc] initVectorWithX:dimx y:dimy] autorelease];
}
- (Vector*)add:(Vector*)q
{
return [Vector vectorWithX:(self.x + q.x) y:(self.y + q.y)];
}
...
Vector *v1 = [[Vector alloc] initVector];
Vector *v2 = [[Vector alloc] initVector];
Vector *vtotal = [v1 add:v2];
...
[v1 release];
[v2 release];
Personally, I would handle v1/v2 with autorelease because I think it makes the code more maintainable and understandable, but there are other schools of thought:
Vector *v1 = [[[Vector alloc] initVector] autorelease];
Vector *v2 = [[[Vector alloc] initVector] autorelease];
Vector *vtotal = [v1 add:v2];
精彩评论