Objective-C abstract pool not releasing an object

I am very new to Objective-C and have read memory management. I tried to play a little with NSAutoreleasePool, but somehow it won't release my object.

I have a class with setter and getter that basically sets the name NSString *. After releasing the pool, I tried the NSLog object and it still works, but I think it should not be?

@interface TestClass : NSObject
{
    NSString *name;
}

- (void) setName: (NSString *) string;
- (NSString *) name;


@end

@implementation TestClass   

- (void) setName: (NSString *) string
{
        name = string;
}  

- (NSString *) name
{
    return name;
}

@end

int main (int argc, const char * argv[]) {

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

TestClass *var = [[TestClass alloc] init];

[var setName:@"Chris"];
[var autorelease];
[pool release];

// This should not be possible?
NSLog(@"%@",[var name]);


return 0;
}
+3
source share
2 answers

. -, copy, retain , name. , , , .

- (void) setName: (NSString*) aName {
    if( name != aName ) {
        if( name ) [name release];
        name = [aName retain];    // or copy
    }
}

.

, , dealloc:

- (void) dealloc {
    self.name = nil;
    [super dealloc];
}

, , , , . , , (var), , , . ( , (auto) release nil).

+2

var, , , , . - , , . , name .

+5

Source: https://habr.com/ru/post/1744501/


All Articles