{"id":85,"date":"2011-03-10T12:40:56","date_gmt":"2011-03-10T16:40:56","guid":{"rendered":"http:\/\/www.joshmatthews.net\/blog\/?p=85"},"modified":"2011-03-10T12:41:35","modified_gmt":"2011-03-10T16:41:35","slug":"nsitimer-anti-pattern","status":"publish","type":"post","link":"https:\/\/www.joshmatthews.net\/blog\/2011\/03\/nsitimer-anti-pattern\/","title":{"rendered":"nsITimer anti-pattern"},"content":{"rendered":"<p>I&#8217;ve filed <a href=\"http:\/\/bugzil.la\/640629\">bug 640629<\/a> to address an intermittent source of orange: incorrect nsITimer creation.  I first ran across it while working on <a href=\"http:\/\/bugzil.la\/508128\">making httpd.js collect garbage more frequently<\/a>, a task which quickly turned into orange whack-a-mole as more and more problematic test constructs popped out of the nether.  Mounir Lamouri (volkmar) recently fixed another instance of the nsITimer problem, so I thought I&#8217;d address it in public and do some education.<\/p>\n<p>When you see a construct like this, you should be wary:<\/p>\n<pre lang=\"javascript\">function doSomething() {\r\n  var timer = Cc[\"@mozilla.org\/timer;1\"].createInstance(Ci.nsITimer);\r\n  timer.initWithCallback(callback, delay, timer.TYPE_ONE_SHOT);\r\n}<\/pre>\n<p>There&#8217;s a common misconception that timers retain an extra reference that is released after they fire.  This is false.  If a timer is created and stored in a locally-scoped variable and the scope is exited, the timer is at risk of being garbage-collected before the timer fires.  To combat this, store a reference to the timer elsewhere &#8211; a member of an object that outlives the current scope, a global variable, it doesn&#8217;t matter.  Do your part &#8211; save a timer&#8217;s life today.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve filed bug 640629 to address an intermittent source of orange: incorrect nsITimer creation. I first ran across it while working on making httpd.js collect garbage more frequently, a task which quickly turned into orange whack-a-mole as more and more &hellip; <a href=\"https:\/\/www.joshmatthews.net\/blog\/2011\/03\/nsitimer-anti-pattern\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,15],"tags":[],"class_list":["post-85","post","type-post","status-publish","format-standard","hentry","category-code","category-mozilla"],"_links":{"self":[{"href":"https:\/\/www.joshmatthews.net\/blog\/wp-json\/wp\/v2\/posts\/85","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.joshmatthews.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.joshmatthews.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.joshmatthews.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.joshmatthews.net\/blog\/wp-json\/wp\/v2\/comments?post=85"}],"version-history":[{"count":6,"href":"https:\/\/www.joshmatthews.net\/blog\/wp-json\/wp\/v2\/posts\/85\/revisions"}],"predecessor-version":[{"id":91,"href":"https:\/\/www.joshmatthews.net\/blog\/wp-json\/wp\/v2\/posts\/85\/revisions\/91"}],"wp:attachment":[{"href":"https:\/\/www.joshmatthews.net\/blog\/wp-json\/wp\/v2\/media?parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joshmatthews.net\/blog\/wp-json\/wp\/v2\/categories?post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joshmatthews.net\/blog\/wp-json\/wp\/v2\/tags?post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}