{"id":4981,"date":"2014-03-30T17:28:15","date_gmt":"2014-03-30T17:28:15","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/problem-about-unsafe-unretained-collection-of-common-programming-errors\/"},"modified":"2014-03-30T17:28:15","modified_gmt":"2014-03-30T17:28:15","slug":"problem-about-unsafe-unretained-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/problem-about-unsafe-unretained-collection-of-common-programming-errors\/","title":{"rendered":"problem about unsafe-unretained-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/f5b9231dfa341d0e8b9ed4d54be73903?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nAnders Sj\u00f6qvist<br \/>\nios objective-c automatic-ref-counting objective-c-runtime unsafe-unretained<br \/>\nI&#8217;m new to Objective-C and ARC, and have been searching and reading for hours without finding an answer. The code does what I want it to do, but I want to know that it doesn&#8217;t rely on favorable conditions. Here&#8217;s a simplified version of my code:+(void)foo {Class *classes = (__unsafe_unretained Class *)malloc(sizeof(Class) * someValue);\/\/ Perform work without complicated memory managementfree(classes); }I can&#8217;t do much about the structure I&#8217;m allocating. It&#8217;s filled by objc_getClassList. Unfortun<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/7e6e0e2b73358e47e0b7f83f8111f75b?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nMattDiPasquale<br \/>\nobjective-c automatic-ref-counting retaincount unsafe-unretained<br \/>\nLet&#8217;s say I want to create a temporary variable, e.g.:To point to another long-living variable:__unsafe_unretained UIView *tableHeaderView = self.tableView.tableHeaderView;To point to an object I just created.__unsafe_unretained UIView *tableHeaderView = [[UIView alloc] init];These temporary variables don&#8217;t need to be retained because the objects they point to are guaranteed to keep positive retain counts for as long as the temporary variables are in scope. So, should I declare them as __unsafe_<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/60351b310de50f48e78c187451ede693?s=128&amp;d=identicon&amp;r=PG\" \/><br \/>\nPang<br \/>\nobjective-c automatic-ref-counting unsafe-unretained<br \/>\nProblem:I have an __unsafe_unretained id pointer that points to an already released object. So far so good, as long as I do not &#8220;use&#8221; the pointer at all (in particular, I do not call any method through the pointer). However, when I try to return its value from a method, it crashes, even if I have explicitly specified that the return value has the type __unsafe_unretained id. Why is that? I thought if I use __unsafe_unretained, it would not call methods like retain \/ release \/ autorelease at all?<\/li>\n<\/ul>\n<p>Web site is in building<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Anders Sj\u00f6qvist ios objective-c automatic-ref-counting objective-c-runtime unsafe-unretained I&#8217;m new to Objective-C and ARC, and have been searching and reading for hours without finding an answer. The code does what I want it to do, but I want to know that it doesn&#8217;t rely on favorable conditions. Here&#8217;s a simplified version of my code:+(void)foo {Class *classes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4981","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4981"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4981\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}