Handling memory leaks in factory methods

I am developing an objective C structure that will ship as a static library at the end. But when I integrate this library into the actual application (adding a static library) into the leak tools, I see that some memory leaks are present.

Here is an example script.

@implementation Test

@synthesize testNumber

+(Test) createTestInstance {

    Test *test = [[Test alloc] init];
    test.testNumber = [[NSDecimerNumber alloc] initWithInt:1];

    return test;
}

-(void) dealloc {
    [testNumber release];
}

@end

Although I release the testNumber variable in dealloc, I see a memory leak in the Leaks tool in the allocation position. What could be here?

In addition, since this is a library provided to the user for invocation, is it better to release this variable from the library code?

thank

+3
source share
1 answer

. testNumber , :

test.testNumber = [[NSDecimerNumber alloc] initWithInt:1];

alloc-init . , :

test.testNumber = [[[NSDecimerNumber alloc] initWithInt:1] autorelease];

, testNumber dealloc.

, , createTestInstance - Test, ( , "alloc", "new", "copy" "mutableCopy" , ):

+ (id)createTestInstance {

    Test *test = [[[self alloc] init] autorelease];
    test.testNumber = [[[NSDecimerNumber alloc] initWithInt:1] autorelease];

    return test;
}

, @Josh Caswell, id . Objective-C :

id , .

, self alloc-init , (self , ).

+11

All Articles