1/*
2 * Copyright (c) 2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20#import <Foundation/Foundation.h>
21#import <objc/runtime.h>
22#import <objc/objc-auto.h>
23#import "auto_zone.h"
24
25// CONFIG GC -C99 -lauto
26
27static BOOL finalized = NO;
28
29@interface FinalizingObject : NSObject
30- (void)finalize;
31@end
32
33@implementation FinalizingObject
34- (void)finalize {
35    finalized = YES;
36    [super finalize];
37}
38@end
39
40static int uniqueKey;
41
42static id createReferences() {
43    // <rdar://problem/6463922>
44    id object = @"Constant Object";
45    id value = [FinalizingObject new];
46    objc_setAssociatedObject(object, &uniqueKey, value, OBJC_ASSOCIATION_ASSIGN);
47    return object;
48}
49
50static void breakReferences(id object) {
51    objc_setAssociatedObject(object, &uniqueKey, nil, OBJC_ASSOCIATION_ASSIGN);
52}
53
54int main(int argc, char *argv[]) {
55    id object = createReferences();
56    auto_collect(auto_zone(), AUTO_COLLECT_FULL_COLLECTION|AUTO_COLLECT_SYNCHRONOUS, NULL);
57    if (finalized) {
58        printf("Failure 1\n");
59        return 1;
60    }
61    breakReferences(object);
62    auto_collect(auto_zone(), AUTO_COLLECT_FULL_COLLECTION|AUTO_COLLECT_SYNCHRONOUS, NULL);
63    if (finalized) {
64        printf("%s: Success\n", argv[0]);
65        return 0;
66    } else {
67        printf("Failure 2\n");
68        return 2;
69    }
70}
71