1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
/*
* Copyright (c) 2008 - 2009
* Jonathan Schleifer <js@webkeks.org>
*
* All rights reserved.
*
* This file is part of libobjfw. It may be distributed under the terms of the
* Q Public License 1.0, which can be found in the file LICENSE included in
* the packaging of this file.
*/
#include "config.h"
#include <stdio.h>
#import "OFAutoreleasePool.h"
#ifndef _WIN32
#define ZD "%zd"
#else
#define ZD "%u"
#endif
int inits;
int retains;
int releases;
IMP init;
IMP retain;
IMP release;
@interface TestObject: OFObject
- init;
- retain;
- (void)release;
@end
@implementation TestObject
- init
{
id ret;
inits++;
ret = init(self, _cmd);
printf("New %s with retain cnt " ZD "\n", [self className],
[ret retainCount]);
return ret;
}
- retain
{
id ret;
retains++;
ret = retain(self, _cmd);
printf("Retaining %s to " ZD "\n", [self className], [ret retainCount]);
return ret;
}
- (void)release
{
releases++;
printf("Releasing %s to " ZD "\n", [self className],
[self retainCount] - 1);
release(self, _cmd);
}
@end
int
main()
{
inits = retains = releases = 0;
init = [OFObject replaceMethod: @selector(init)
withMethodFromClass: [TestObject class]];
retain = [OFObject replaceMethod: @selector(retain)
withMethodFromClass: [TestObject class]];
release = [OFObject replaceMethod: @selector(release)
withMethodFromClass: [TestObject class]];
OFObject *o1, *o2, *o3;
OFAutoreleasePool *pool1, *pool2;
o1 = [[[OFObject alloc] init] autorelease];
pool1 = [[OFAutoreleasePool alloc] init];
o2 = [[[OFObject alloc] init] autorelease];
[pool1 releaseObjects];
o2 = [[[OFObject alloc] init] autorelease];
pool2 = [[OFAutoreleasePool alloc] init];
o3 = [[[OFObject alloc] init] autorelease];
[pool1 release];
printf("inits: %02d\nretains: %02d\nreleases: %02d\n",
inits, retains, releases);
return (inits == 17 && retains == 5 && releases == 16 ? 0 : 1);
}
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|