Returning objects with autorelease but I still leak memory

Posted by gok on Stack Overflow See other posts from Stack Overflow or by gok
Published on 2010-03-12T19:46:11Z Indexed on 2010/03/12 19:47 UTC
Read the original article Hit count: 611

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.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone