在目标C中保存char *和intptr_t

我想保存一个const char*和一个const intptr_t以便我可以保存dynamic库加载。 我正在使用下面的代码来loggingdynamic库加载:

 + (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: %p\n\n", 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 %s\n\n", 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的结果,更糟的是,因为你需要调用该函数 。 它做的事情。 它不只是返回一个指针。 没有这个调用,那个指针就完全没用了。