在目标 C 中保存字符* 和intptr_t



我想保存一个const char*和一个const intptr_t,以便我可以保存动态库加载。我目前正在使用以下代码来记录动态库加载:

+ (void)startRecording {
//    NSArray *oldLibs = [NSKeyedUnarchiver unarchiveObjectWithFile:[self returnFilePathWithName:@"dynamic_libraries"]];
//    NSLog(@"start recording");
//    NSLog(@"oldLibs: %@", oldLibs);
    _dyld_register_func_for_add_image(&image_added);
    _dyld_register_func_for_remove_image(&image_removed);
}
static void image_added(const struct mach_header *mh, intptr_t slide)
{
    _print_image(mh, true);
}
static void image_removed(const struct mach_header *mh, intptr_t slide)
{
    _print_image(mh, false);
}
static void _print_image(const struct mach_header *mh, bool added)
{
    Dl_info image_info;
    int result = dladdr(mh, &image_info);
    if (result == 0) {
        printf("Could not print info for mach_header: %pnn", mh);
        return;
    }
    const char *image_name = image_info.dli_fname;
    const intptr_t image_base_address = (intptr_t)image_info.dli_fbase;
    const char *log = added ? "Added" : "Removed";
    printf("%s: 0x%02lx %snn", log, image_base_address, image_name);
    //    NSString *imageName = [NSString stringWithUTF8String:image_name];
    //    NSNumber *baseAddress = [NSNumber numberWithInteger:image_base_address];
}

我知道我可以将image_nameimage_base_address转换为Objective-C对象,但是我应该在哪里/何时保存它们?

每次运行程序时,指针通常具有不同的值,并且尝试保存和还原它们通常会导致彻底的灾难。

如果你想保存和恢复_dyld_register_func_for_add_image的结果,那就更糟了,因为你需要调用该函数。它做事。它不只是返回一个指针。如果没有调用,该指针将完全无用。

最新更新