开发者

Objective-C code not deallocating memory!

开发者 https://www.devze.com 2022-12-12 11:50 出处:网络
I\'m trying to learn Objective-C. I almost finished one excercise but it is not deallocating the memory:

I'm trying to learn Objective-C. I almost finished one excercise but it is not deallocating the memory:

This is is what I have:

void PrintPolygonInfo() {
    NSLog(@"--------------------");
    NSLog(@" PRINT POLYGON INFO");
    NSLog(@"--------------------");
    NSMutableArray *array = [[NSMutableArray alloc] init];
    PolygonShape *p1 = [[PolygonShape alloc] initWithNumberOfSides:4 minimumNumberOfSides:3 maximumNumberOfSides:7];
    PolygonShape *p2 = [[PolygonShape alloc] initWithNumberOfSides:6 minimumNumberOfSides:5 maximumNumberOfSides:9];
    PolygonShape *p3 = [[PolygonShape alloc] initWithNumberOfSides:12 minimumNumberOfSides:9 maximumNumberOfSides:12];
    [array addObject:p1];
    [array addObject:p2];
    [array addObject:p3];
    // Log the descriptions
    for (id shape in array) {
        NSLog(@"%@", shape);
    }
    // Test the constraints
    for (PolygonShape *shape in array) {
        [shape setNumberOfSides:10]开发者_StackOverflow;
    }
    [p1 release];
    [p2 release];
    [p3 release];
}

This is the dealloc():

- (void) dealloc {
    NSLog(@"Deallocated!!!");
    [super dealloc];
}

And this are the results:

2009-11-19 06:58:17.030 Assignment 1B[5441:a0f] --------------------
2009-11-19 06:58:17.030 Assignment 1B[5441:a0f]  PRINT POLYGON INFO
2009-11-19 06:58:17.031 Assignment 1B[5441:a0f] --------------------
2009-11-19 06:58:17.031 Assignment 1B[5441:a0f] init: Retain count of 1.
2009-11-19 06:58:17.032 Assignment 1B[5441:a0f] init: Retain count of 1.
2009-11-19 06:58:17.032 Assignment 1B[5441:a0f] init: Retain count of 1.
2009-11-19 06:58:17.033 Assignment 1B[5441:a0f] Hello I am a 4-sided polygon (aka a Square) with angles of 90 degrees (1.570796 radians).
2009-11-19 06:58:17.033 Assignment 1B[5441:a0f] Hello I am a 6-sided polygon (aka a Hexagon) with angles of 120 degrees (2.094395 radians).
2009-11-19 06:58:17.034 Assignment 1B[5441:a0f] Hello I am a 12-sided polygon (aka a Dodecagon) with angles of 150 degrees (2.617994 radians).
2009-11-19 06:58:17.034 Assignment 1B[5441:a0f] Invalid number of sides: 10 is greater than the maximum of 7 allowed
2009-11-19 06:58:17.035 Assignment 1B[5441:a0f] Invalid number of sides: 10 is greater than the maximum of 9 allowed

As you can see, it is not printing the 'Deallocated!!!" message:

Can anyone tell me what I am doing wrong please?

Thanks in advance


The array retains the objects it contains. The objects can only be deallocated once they are removed from the array, or the array is released.


You're leaking your array. You create it with alloc/init, but never release it. Either release it, or create it with [NSMutableArray array].

Also, the dealloc method is called on objects when they're released. However, it looks like you're using a function (not a method of an object) to do that stuff. How is the rest of your code set up?


Note that if you're application terminates, the dealloc method might not be called. The application's memory is cleared anyways and OSX might decide it's faster to just terminate the app and clear the memory without going through all deallocs.

This is documented in the NSObject reference.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号