debug


//
//  PTObject.h


#import <Cocoa/Cocoa.h>


@interface PTObject : NSObject 

    -(
void) trace:(const char*) op;

@end



//
//  PTObject.m


#import 
"PTObject.h"


@implementation PTObject
-(id) init{
    self 
=[super init];
    [self trace:
" init'd"];
    
return self;
}




- (id) retain {
    self 
= [super retain];
    [self trace:
"retain'd"];
    
return self;
}


-(void) release {
    [self trace: 
"release"];
    [super release];
}



-(id)autorelease {
    self
=[super autorelease];
    [self trace:
"autorelease'd"];
    
return self;
}



-(void)dealloc {
    [self trace:
"dealloc"];
    [super dealloc];
}



-(void)trace:(const char*) op {
   printf("%-32s/t<%s %p> %d/n",op,object_getClassName(self),self,[self retainCount]);

    fflush(stdout);
}




@end
 

//
//  PTAutoreleasePool.h
//

#import 
<Cocoa/Cocoa.h>



@interface PTAutoreleasePool : NSAutoreleasePool 


@end


//
//  PTAutoreleasePool.m
//  pool

#import <Foundation/NSDebug.h>
#import 
"PTAutoreleasePool.h"
#import 
"PTObject.h"


@implementation PTAutoreleasePool
+(void) initialize {
    
static BOOL twice;
    
    
if (!twice) {
        [PTObject poseAsClass:[PTObject superclass]];
        [self poseAsClass:[self superclass]];
        twice 
=YES;
    }

    
}


-(id) init {
    self 
=[super init];
    [self trace:
"pool init'd"];
    
return self;
}



-(void)release{
    [self trace:
"pool release"];
    [[self 
class] showPools];
    [super release];
}




-(void)dealloc{
    [self trace:
"pool dealloc"];
    [super dealloc];
    
}


@end

//
//  main.m
//  DebugDemo
//


#import 
<Cocoa/Cocoa.h>
#import 
"PTAutoreleasePool.h"

static id maker() 
    
return [[NSObject new] autorelease];
}


static id keeper() {
    NSAutoreleasePool 
*pool = [[NSAutoreleasePool alloc] init];
    id obj 
= maker();
    [obj retain];
    [pool release];
    
return [obj autorelease];
}


static id array;
static void fillContainer() {
    array 
= [NSMutableArray arrayWithObject:keeper()];
    [array retain];
}


static id useContainer() {
    id obj 
=[[[ array lastObject] retain] autorelease];
    [array removeLastObject];
    [array autorelease];
    
return obj;
}



int main(int argc, char *argv[])
{
    [PTAutoreleasePool initialize];
    NSAutoreleasePool 
*global = [[NSAutoreleasePool alloc] init];
    NSAutoreleasePool 
*local = [[NSAutoreleasePool alloc] init];
    fillContainer();
    [local release];
    useContainer();
    [
global release];
    exit(
0);
    
return 0;
    
    
//return NSApplicationMain(argc,  (const char **) argv);
}





[Session started at 
2006-07-29 08:51:07 +0800.]
pool init
'd                         <NSAutoreleasePool 0x306cd0> 1   global
pool init'd                         <NSAutoreleasePool 0x306db0> 1 local
pool init'd                         <NSAutoreleasePool 0x306e00> 1 keeper
 init'd                             <NSObject 0x304150> 1   maker
autorelease'd                       <NSObject 0x304150> 1
retain'd                            <NSObject 0x304150> 2   keeper
pool release                        <NSAutoreleasePool 0x306e00> 1   pool
DebugDemo[
1014] Pool (level = 0): 1 objects ====================
DebugDemo[
1014]     0x304150 (NSObject)
DebugDemo[
1014] Pool (level = 1): 0 objects ====================
DebugDemo[
1014] Pool (level = 2): 0 objects ====================
release                             
<NSObject 0x304150> 2
autorelease
'd                       <NSObject 0x304150> 1
retain'd                            <NSObject 0x304150> 2      array
autorelease'd                       <NSCFArray 0x307180> 1
pool release                        <NSAutoreleasePool 0x306db0> 1   local
DebugDemo[
1014] Pool (level = 0): 2 objects ====================
DebugDemo[
1014]     0x307180 (NSCFArray)
DebugDemo[
1014]     0x304150 (NSObject)
DebugDemo[
1014] Pool (level = 1): 0 objects ====================
release                             
<NSObject 0x304150> 2
retain
'd                            <NSObject 0x304150> 2      useContainer
autorelease'd                       <NSObject 0x304150> 2
release                             <NSObject 0x304150> 2      array
autorelease
'd                       <NSCFArray 0x307180> 1
pool release                        <NSAutoreleasePool 0x306cd0> 1      global
DebugDemo[
1014] Pool (level = 0): 2 objects ====================
DebugDemo[
1014]     0x307180 (NSCFArray)
DebugDemo[
1014]     0x304150 (NSObject)
release                             <NSObject 0x304150> 1
dealloc                             <NSObject 0x304150> 1

DebugDemo has exited with status 
0.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章