ios - How to handle owning references to GCD dispatch queues in malloc'ed memory -


i adapting client linux app (in c) keeps linked list of malloc(ed) structs, each of holds pthread (posix thread).

apple discourages using posix threads, i'm adapting use concurrent gcd dispatch queues instead.

the source going maintained across both linux , ios, i'd limit structural changes.

to keep changes minimum, going replace linked list of structs containing pthreads single malloced struct contains pointer concurrent gcd dispatch queue created dispatch_queue_create().

however, occurred me that might not work due arc. default, arc doesn't know owning references object types in structs, not created malloc(). isn't there way arc track strong references contained in structs and/or arbitrary memory blocks? have vague memory of apple adding such thing, can't seem find it.

you can if compile objective-c++ source code.

#import <foundation/foundation.h> struct data {     dispatch_queue_t queue; }; 

objective-c

$ clang -x objective-c -fobjc-arc -c test.m test.m:3:19: error: arc forbids objective-c objects in struct         dispatch_queue_t queue;                          ^ 1 error generated. 

objective-c++

$ clang -x objective-c++ -fobjc-arc -c test.m 

no errors.

please take @ clang objective-c automatic reference counting document.

a program ill-formed if declares member of c struct or union have nontrivially ownership-qualified type.

this restriction not apply in objective-c++.

updated

you can c++ new/delete. or if want use malloc, need call destructor of struct.

#import <foundation/foundation.h> #import <new>  @interface test : nsobject @property (nonatomic) dispatch_queue_t queue; @end  @implementation test - (instancetype)init {     self = [super init];     if (self)         self.queue = dispatch_queue_create("test", null);     return self; } - (void)dealloc {     nslog(@"test dealloced"); } @end  struct data {     test* test; };  int main() {     nslog(@"stack ===================");     {         data d;         d.test = [[test alloc] init];     }     nslog(@"stack end ===============");      nslog(@"new =====================");     {         data* d = new data();         d->test = [[test alloc] init];         delete d;     }     nslog(@"new end =================");      nslog(@"placement new ===========");     {         void* p = malloc(sizeof(data));         data* d = new(p) data();         d->test = [[test alloc] init];         d->~data();         free(d);     }     nslog(@"placement new end =======");      return 0; } 

result

stack =================== test dealloced stack end =============== new ===================== test dealloced new end ================= placement new =========== test dealloced placement new end ======= 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -