{"id":5001,"date":"2014-03-30T17:43:58","date_gmt":"2014-03-30T17:43:58","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/malloc-object-lifetime-with-__unsafe_unretained-typecast-collection-of-common-programming-errors\/"},"modified":"2014-03-30T17:43:58","modified_gmt":"2014-03-30T17:43:58","slug":"malloc-object-lifetime-with-__unsafe_unretained-typecast-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/malloc-object-lifetime-with-__unsafe_unretained-typecast-collection-of-common-programming-errors\/","title":{"rendered":"malloc object lifetime with __unsafe_unretained typecast-Collection of common programming errors"},"content":{"rendered":"<blockquote>\n<p>Does Class behave differently from other kinds of Objective-C data types? Should I expect something else from it?<\/p>\n<\/blockquote>\n<p><code>Class<\/code> is a type that can hold a pointer to any class object, just like how <code>id<\/code> is a type that can hold a pointer to any object, and how <code>NSString *<\/code> is a type that can hold a pointer to any object that is an instance of <code>NSString<\/code> (or subtype thereof). Class objects are (one kind of) objects, and work in basically the same ways that other objects do. The type <code>Class<\/code> can be used just like other object-pointer types.<\/p>\n<blockquote>\n<p>What kind of pointer is classes? I guess it references an array of unknown size (a non-object pointer), but ARC still cares about it since the code won&#8217;t compile without that cast. It looks like a strong pointer, but I don&#8217;t know what it would be a strong pointer to.<\/p>\n<\/blockquote>\n<p><code>classes<\/code> is a pointer to <code>Class<\/code>, i.e. a pointer to a pointer to a class object. You are using it here to point to the first member of an array of <code>Class<\/code>, which is correct. <code>classes<\/code> is not an object pointer, but it is a pointer to an object pointer; that&#8217;s why ARC cares. It&#8217;s just like if you cast the result of <code>malloc<\/code> to <code>id *<\/code> or <code>NSString **<\/code>. When you don&#8217;t specify a qualifier on <code>Class<\/code>, it is implicitly <code>__unsafe_unretained<\/code>. So <code>classes<\/code> is already a pointer to an unretained object pointer.<\/p>\n<blockquote>\n<p>Are there strong pointers to the objects in the array at all, or are the objects themselves strong pointers? If not, does ARC still work, or should I revert to some MRC conventions?<\/p>\n<p>Will there be an automated call to dealloc? When, and how can the compiler ensure that that happens?<\/p>\n<\/blockquote>\n<p><code>Class<\/code> holds pointers to class objects. Class objects are basically singletons; they are not reference-counted. They generally live for the duration of the program, except perhaps if the class is unloaded. It&#8217;s not necessary to maintain an explicit strong reference to the class object; the class will stay around.<\/p>\n<blockquote>\n<p>Is it better practice to set classes to NULL, and only then do the call to free? Or should I iterate over the array and do something about every object, such as setting them to nil?<\/p>\n<\/blockquote>\n<p>You obviously shouldn&#8217;t set <code>classes<\/code> to NULL <em>before<\/em> calling free(); that way, calling <code>free(classes);<\/code> won&#8217;t do anything. Since the elements of this array are <code>__unsafe_unretained<\/code>, there is no point to setting them to anything before freeing the memory; it will not make a difference.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Does Class behave differently from other kinds of Objective-C data types? Should I expect something else from it? Class is a type that can hold a pointer to any class object, just like how id is a type that can hold a pointer to any object, and how NSString * is a type that can [&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-5001","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5001","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=5001"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5001\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}