{"id":1736,"date":"2008-06-07T10:31:09","date_gmt":"2008-06-07T14:31:09","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=1736"},"modified":"2008-06-07T10:31:12","modified_gmt":"2008-06-07T14:31:12","slug":"garbage-collection-and-the-optimizer","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2008\/06\/07\/garbage-collection-and-the-optimizer\/","title":{"rendered":"Garbage Collection and the Optimizer"},"content":{"rendered":"<p>There&rsquo;s an interesting edge case in the Objective-C garbage collector that&rsquo;s being discussed <a href=\"http:\/\/lists.apple.com\/archives\/Cocoa-dev\/2008\/Jun\/msg00603.html\">on Cocoa-dev<\/a>. The collector treats variables on the stack as rooted, but the contents of the stack may not match what the programmer is expecting. If gcc&rsquo;s optimizer determines that a particular variable will no longer be accessed, it can replace it on the stack before it goes out of scope in the Objective-C source. Ordinarily this is safe, but it can cause problems when dropping down to the C level. If the variable points to an <code>NSData<\/code>, the object can be collected while a pointer to its contents is still in use. <a href=\"http:\/\/lists.apple.com\/archives\/Cocoa-dev\/2008\/Jun\/msg00650.html\">Mike Ash<\/a> describes it nicely:<\/p>\n<blockquote cite=\"http:\/\/lists.apple.com\/archives\/Cocoa-dev\/2008\/Jun\/msg00650.html\">\n<p>The problem here is that you&rsquo;re expecting one pointer to keep a <em>different<\/em> pointer live, which the GC does not make any guarantees about.\n<\/p>\n<p>Basically, under GC, my impression is that it should be considered invalid to return a pointer to a caller which depends on the lifetime of the parent object.<\/p><\/blockquote>\n<p>Unfortunately, there isn&rsquo;t a simple fix because <code>NSData<\/code>&rsquo;s contents are not always a simple block of collectable memory.<\/p>","protected":false},"excerpt":{"rendered":"<p>There&rsquo;s an interesting edge case in the Objective-C garbage collector that&rsquo;s being discussed on Cocoa-dev. The collector treats variables on the stack as rooted, but the contents of the stack may not match what the programmer is expecting. If gcc&rsquo;s optimizer determines that a particular variable will no longer be accessed, it can replace it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-1736","post","type-post","status-publish","format-standard","hentry","category-programming-category"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/1736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=1736"}],"version-history":[{"count":0,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/1736\/revisions"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=1736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=1736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=1736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}