Skip to content

Instantly share code, notes, and snippets.

@Graham--M

Graham--M/main.m Secret

Created March 14, 2021 14:16
Show Gist options
  • Save Graham--M/98750a153137872dc0a21d0ab55826d5 to your computer and use it in GitHub Desktop.
Save Graham--M/98750a153137872dc0a21d0ab55826d5 to your computer and use it in GitHub Desktop.
#import <objc/runtime.h>
#import <objc/objc-arc.h>
#include <pthread.h>
/* ----- Mimimal Class ----- */
__attribute__((objc_root_class))
@interface Test { id isa; }
+ (Class)class;
+ (id)new;
- (void)dealloc;
- (id)autorelease;
- (id)retain;
- (void)release;
@end
@implementation Test
+ (Class)class { return self; }
+ (id)new { return class_createInstance(self, 0); }
- (void)dealloc { object_dispose(self); }
- (id)autorelease { return objc_autorelease(self); }
- (id)retain { return objc_retain(self); }
- (void)release { objc_release(self); }
- (void)_ARCCompliantRetainRelease {}
@end
/* ----- Test Program ----- */
id ptr = NULL; // Will hold the weak reference.
void *thread_func(void *x_void_ptr) {
id obj = objc_loadWeakRetained(&ptr);
return NULL;
}
int main(void) {
Test *test = [Test new];
objc_storeWeak(&ptr, test);
pthread_t the_thread;
if (pthread_create(&the_thread, NULL, thread_func, NULL)) {
return 1;
}
[test release];
test = NULL;
if (pthread_join(the_thread, NULL)) {
return 2;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment