English 中文(简体)
NSGarbage Colectors 是否禁用了Colector for Pointer: 调用平衡 。
原标题:Are NSGarbageCollector s disableCollectorForPointer: calls balanced by enableCollectorForPointer: calls?
最佳回答

我想我找到了答案。 垃圾收集器的源代码似乎不存在, 但信头文件声明 < code> NSGarbage Colector 的界面, < code> NSGarbage Colector.h 包含以下内容: < code> Foundation. framework

// references outside the heap, globals, and the stack, e.g. unscanned memory, malloc memory, must be tracked by the collector
- (void)disableCollectorForPointer:(void *)ptr;     // this pointer will not be collected...
- (void)enableCollectorForPointer:(void *)ptr;      // ...until this (stacking) call is made

请注意“ 坚固” 的“ 堆积” < / 坚固” 评论 - 我想这意味着电话确实被计数了? 更多的证据仍然令人欢迎!

<强> 更新:

我决定用一个小测试程序来测试我的假设(“强力”格克布里奇特.m

#import <Foundation/Foundation.h>

@interface PJGarbageCollectionTest : NSObject
@end

@implementation PJGarbageCollectionTest

- (id)init
{
    self = [super init];
    if (!self) return nil;
    NSLog(@"%@ -init", self);
    return self;
}

- (void)finalize
{
    NSLog(@"%@ -finalize", self);
    [super finalize];
}

@end

static void* ext_ptr1 = NULL;
static void* ext_ptr2 = NULL;

static void create()
{
    PJGarbageCollectionTest* test = [[PJGarbageCollectionTest alloc] init];
    [[NSGarbageCollector defaultCollector] disableCollectorForPointer:test];
    ext_ptr1 = test;
    [[NSGarbageCollector defaultCollector] disableCollectorForPointer:test];
    ext_ptr2 = test;
}

static void killref(void** ext_ptr)
{
    [[NSGarbageCollector defaultCollector] enableCollectorForPointer:*ext_ptr];
    *ext_ptr = NULL;
}


int main()
{
    NSLog(@"collector: %@", [NSGarbageCollector defaultCollector]);
    create();
    NSLog(@"Collecting with 2 external references");
    [[NSGarbageCollector defaultCollector] collectExhaustively];
    killref(&ext_ptr1);
    NSLog(@"Collecting with 1 external reference");
    [[NSGarbageCollector defaultCollector] collectExhaustively];
    killref(&ext_ptr2);
    NSLog(@"Collecting with 0 external references");
    [[NSGarbageCollector defaultCollector] collectExhaustively];
    return 0;
}

gcc -fobjc-gc- only -g -Wall -Wextra -ObjC gcbridgetest.m -Framework Foundation -o gcbridgetest 编译,以 ./gcbridgetest 运行,它提供以下输出,确认启用/禁用聚合器Forpointer:电话确实被计数 :

2012-06-12 16:08:08.278 gcbridgetest[29712:903] collector: <NSGarbageCollector: 0x20000f420>
2012-06-12 16:08:08.281 gcbridgetest[29712:903] <PJGarbageCollectionTest: 0x20000ee60> -init
2012-06-12 16:08:08.284 gcbridgetest[29712:903] Collecting with 2 external references
2012-06-12 16:08:08.285 gcbridgetest[29712:903] Collecting with 1 external reference
2012-06-12 16:08:08.286 gcbridgetest[29712:903] Collecting with 0 external references
2012-06-12 16:08:08.286 gcbridgetest[29712:903] <PJGarbageCollectionTest: 0x20000ee60> -finalize
问题回答

暂无回答




相关问题
GarbageCollector, Dispose or static Methods?

I developed a few classes last month. They grow big (round 30-40 Methods each class). I never take a thought of Memory Leaks, GarbageColletor or something like this (I must say this is my first own ...

Can t free memory of NSData object

i m new to xcode / cocoa and even objective-c thus my question might be stupid. Im trying to write a program that will hash files in a folder. I looked around and found a way to load a file via a ...

Alternative to Java

I need an alternative to Java, because I am working on a genetics-calculation project. It takes a lot of memory and the most of the cpu time. And therefore it won´t work when I deploy it on a server, ...

Question about the garbage collector in .NET (memory leak)

I guess this is very basic but since I m learning .NET by myself, I have to ask this question. I m used to coding in C, where you have to free() everything. In C++/.NET, I have read about the garbage ...

Does garbage collection work with the Go compiler (gc)?

I noticed that garbage collection is not yet implemented in gccgo. http://golang.org/doc/gccgo_install.html#Unimplemented Does the standard Go compiler (gc) support garbage collection yet?

doubts regarding Memory management in .net

I m learning about Memory management in C# from the book "Professional C#" The presence of the garbage collector means that you will usually not worry about objects that you no longer need; ...

热门标签