4

I am coding an image gallery tonight and this led me to a philosophical question

I create a preloaded Image object and set an onload event for it

  ...  onmouseover = function() {
        preloadActive = true;
        imgPreload = new Image();

        imgPreload.onload = function() {
            preloadActive = false;
        }

        imgPreload.src = ...;
      }

Where imgPreload and preloadActive are global variables

Now imagine that a new onmouseover() fires out before onload() executes.

a line of code is run which creates a new image object, the old image object looses the last link and goes to Erebus, and is waiting for garbage collector to eat it.

The question goes here: The old object instance is not destroyed immediately. Its onload event continues to live nobody knows for how long time? Do you have any cross-browser experience about that?

Thank you!

PS: I don't worry about IE6


PPS: What happens if I have an object with setInterval timeout ID inside?

obj.someVar = setInterval(...)

Is setInterval stopped the very moment I execute

obj = {}

?

Dan
  • 55,715
  • 40
  • 116
  • 154
  • Though I might be mistaken, shouldn't you execute DOM related code like onmouseover inside the onload event? Doing so would prevent any issues from bubbling up. Regardless, this is still a good question. – Moses Dec 15 '10 at 21:23
  • 1
    -1, Just for going back to an awful title. There is no such thing as "parallel threads" in (browser) JavaScript (that can access the same objects -- Web Workers are different). Events are asynchronous and blocking. Also this issue is not related to the GC, but merely about object lifetime and if having an event (`onload`) will prevent the object from being un-reachable. –  Dec 15 '10 at 21:28
  • @Moses: do you want to use some mechanism to prevent re-assigning imgPreload variable? "if(preloadActive)return;" But, we should find out the garbage collector behaviour – Dan Dec 15 '10 at 21:43
  • good link http://stackoverflow.com/questions/4324133/javascript-garbage-collection – Dan Dec 15 '10 at 22:04

1 Answers1

3

Well, for starters, global variables aren't eligible for garbage collection (which is usually the cause of memory leak issues and is one of many reasons to avoid using global variables all together).

Having said that, please read this article by Eric Lippert. It's old, but I still think it is quite relevant.

Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
  • Ok, I define a these two variables inside a "slideshow" object – Dan Dec 15 '10 at 21:35
  • Did you read the first paragraph where I say that the article is no longer relevant? – Eric Lippert Dec 16 '10 at 04:04
  • @Eric If you have a problem with your old article being referenced then perhaps you should remove it. Every person who follows this link will see your precious first paragraph. What is the problem? – Josh Stodola Dec 16 '10 at 05:52
  • I just find it curious that you think it is still relevant when it begins by pointing out that it is massively outdated. The JScript garbage collector is no longer anything like the way I described it back in 2003. Why do you believe this article to be relevant to the question? – Eric Lippert Dec 16 '10 at 05:55
  • @Eric Maybe "relevant" was the wrong word to use. I should have rather called it "interesting". – Josh Stodola Dec 16 '10 at 05:59