Objective-C代码没有释放内存!

Dav*_*les 0 memory-leaks objective-c

我正在努力学习Objective-C.我几乎完成了一个练习,但它没有释放内存:

这就是我所拥有的:

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];
    }
    [p1 release];
    [p2 release];
    [p3 release];
}
Run Code Online (Sandbox Code Playgroud)

这是dealloc():

- (void) dealloc {
    NSLog(@"Deallocated!!!");
    [super dealloc];
}
Run Code Online (Sandbox Code Playgroud)

这就是结果:

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
Run Code Online (Sandbox Code Playgroud)

如您所见,它不会打印'Deallocated !!!'消息:

任何人都可以告诉我,我做错了吗?

提前致谢

not*_*oop 7

该数组保留它包含的对象.只有从数组中删除对象或释放数组后,才能释放对象.