Returning objects with autorelease but I still leak memory
- by gok
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 release.