代码之家  ›  专栏  ›  技术社区  ›  smokris

如何以编程方式确定cocoa插件包的垃圾收集设置?

  •  6
  • smokris  · 技术社区  · 15 年前

    在使用Objective-C2的Mac OS X上,插件包可以使用以下三种垃圾收集设置之一进行编译:

    1. 不支持
    2. 支持( -fobjc-gc )
    3. 要求的( -fobjc-gc-only )

    如何以编程方式查询已编译的插件包以确定使用了这三个设置中的哪一个?

    3 回复  |  直到 15 年前
        1
  •  3
  •   Community c0D3l0g1c    7 年前

    根据以下答案 diciu ,您可以使用 Mach-O API . 您必须分析二进制文件中包含的段并搜索 γ-Objc 一个; segment_command 结构允许访问段的标志。

    你也可以看看 ClassDump project . 它有一个非常完整的mach-o解析器。

        2
  •  4
  •   diciu    15 年前

    它是_uuobjc段的一部分,但我不知道有任何API公开它。

    垃圾收集:

    cristi:tmp diciu$ otool -v -o ./a.out 
    ./a.out:
    Contents of (__DATA,__objc_classrefs) section
    00000001000010b0 0x0
    Contents of (__DATA,__objc_imageinfo) section
      version 0
        flags 0x6 OBJC_IMAGE_SUPPORTS_GC
    

    非垃圾收集:

    cristi:tmp diciu$ otool -v -o ./a.out 
    ./a.out:
    Contents of (__DATA,__objc_classrefs) section
    00000001000010b0 0x0
    Contents of (__DATA,__objc_imageinfo) section
      version 0
        flags 0x0
    

    运行时使用专用函数执行此操作: see gc_enforcer and it's use of * _objcInfoRequiresGC*

        3
  •  1
  •   Rob Keniger    15 年前

    尝试使用nsbundle加载包可能是最简单的方法。 -loadAndReturnError: 方法。如果捆绑包由于其GC设置与应用程序中的设置不同而无法加载,您将获得 NSExecutableRuntimeMismatchError .