Markers' events memory leak?

5 messages Options Options
Embed this Post
Permalink
Donal

Markers' events memory leak?

Reply Threaded MoreMore options
Print post
Permalink
Hello,

I have a GeoRSS layer which displays a number of icons equal to the value of a select box e.g. 10,25,50,100.
When the select box changes value, the GeoRSS layer is destroyed and a new layer is displayed with the new number of icons. I notice in IE 6 that the memory used steadily increases as I repeatedly change (up and down)  the value in the select box.

I see that when a marker is initialized,  BROWSER_EVENTS are attached to the marker's element (this.icon.imageDiv)  with a call to OpenLayers.Event.observe(element,eventType,this.handleBrowserEvent.bindAsEventListener(this)); in Events.js attachToElement() function.

If I comment out the call to attachToElement, the memory used stays fairly constant as I change the select box value (but obviously my markers have no events associated with them which isn't much use). This suggests to me that events not being detached/removed properly are the cause of the leak.

So I want to try something like

OpenLayers.Event.stopObserving(this.icon.imageDiv, "mouseover", observer);

for each of the BROWSER_EVENTS as the markers are destroyed but I am not sure what I should put as the value for observer.

I have tried

adding the following propeties to the Events object

this.obs_handles = new Object();
this.obs_handles[element] = new Array();

and then in attachToElement doing

this.obs_handles[element][eventType] = this.handleBrowserEvent.bindAsEventListener(this);
OpenLayers.Event.observe(element, eventType, this.obs_handles[element][eventType]);

and then in Markers.destroy() adding
                OpenLayers.Event.stopObserving(this.icon.imageDiv,"mouseover",this.events.obs_handles[this.icon.imageDiv]["mouseover"]);
OpenLayers.Event.stopObserving(this.icon.imageDiv,"mouseout",this.events.obs_handles[this.icon.imageDiv]["mouseout"]);
OpenLayers.Event.stopObserving(this.icon.imageDiv,"mousedown",this.events.obs_handles[this.icon.imageDiv]["mousedown"]);
etc;

this.events.obs_handles[this.icon.imageDiv] = null;
this.events.obs_handles = null;

but this has no effect.

Am I barking up the wrong tree? Any help/advice will be gratefully received.

Thanks,

Donal
Donal

Re: Markers' events memory leak?

Reply Threaded MoreMore options
Print post
Permalink
I hope that some might find this useful.

I found the following function at http://dev.rubyonrails.org/ticket/5198

// Remove all event handlers from an element
  stopObservingElement: function (element) {
    if (! Event.observers) return;
      for (var i = 0; i < Event.observers.length; ) {
        if (element == Event.observers[i][0]) {
          Event.stopObserving.apply (this, Event.observers[i]);
          Event.observers.splice (i, 1);
        }
        else
          i++;
      }
   }

I added this function to the OpenLayers.Event class.

I now can call OpenLayers.Event.stopObservingElement(this.imageDiv); in the icon destroy() method and the memory leak is kept in check.

Cheers,

Donal



Donal wrote:
Hello,

I have a GeoRSS layer which displays a number of icons equal to the value of a select box e.g. 10,25,50,100.
When the select box changes value, the GeoRSS layer is destroyed and a new layer is displayed with the new number of icons. I notice in IE 6 that the memory used steadily increases as I repeatedly change (up and down)  the value in the select box.

I see that when a marker is initialized,  BROWSER_EVENTS are attached to the marker's element (this.icon.imageDiv)  with a call to OpenLayers.Event.observe(element,eventType,this.handleBrowserEvent.bindAsEventListener(this)); in Events.js attachToElement() function.

If I comment out the call to attachToElement, the memory used stays fairly constant as I change the select box value (but obviously my markers have no events associated with them which isn't much use). This suggests to me that events not being detached/removed properly are the cause of the leak.

So I want to try something like

OpenLayers.Event.stopObserving(this.icon.imageDiv, "mouseover", observer);

for each of the BROWSER_EVENTS as the markers are destroyed but I am not sure what I should put as the value for observer.

I have tried

adding the following propeties to the Events object

this.obs_handles = new Object();
this.obs_handles[element] = new Array();

and then in attachToElement doing

this.obs_handles[element][eventType] = this.handleBrowserEvent.bindAsEventListener(this);
OpenLayers.Event.observe(element, eventType, this.obs_handles[element][eventType]);

and then in Markers.destroy() adding
                OpenLayers.Event.stopObserving(this.icon.imageDiv,"mouseover",this.events.obs_handles[this.icon.imageDiv]["mouseover"]);
OpenLayers.Event.stopObserving(this.icon.imageDiv,"mouseout",this.events.obs_handles[this.icon.imageDiv]["mouseout"]);
OpenLayers.Event.stopObserving(this.icon.imageDiv,"mousedown",this.events.obs_handles[this.icon.imageDiv]["mousedown"]);
etc;

this.events.obs_handles[this.icon.imageDiv] = null;
this.events.obs_handles = null;

but this has no effect.

Am I barking up the wrong tree? Any help/advice will be gratefully received.

Thanks,

Donal
Christopher Schmidt-2

Re: Markers' events memory leak?

Reply Threaded MoreMore options
Print post
Permalink
On Thu, Feb 15, 2007 at 09:29:11PM -0800, Donal wrote:

>
> I hope that some might find this useful.
>
> I found the following function at http://dev.rubyonrails.org/ticket/5198
>
> // Remove all event handlers from an element
>   stopObservingElement: function (element) {
>     if (! Event.observers) return;
>       for (var i = 0; i < Event.observers.length; ) {
>         if (element == Event.observers[i][0]) {
>           Event.stopObserving.apply (this, Event.observers[i]);
>           Event.observers.splice (i, 1);
>         }
>         else
>           i++;
>       }
>    }
>
> I added this function to the OpenLayers.Event class.
>
> I now can call OpenLayers.Event.stopObservingElement(this.imageDiv); in the
> icon destroy() method and the memory leak is kept in check.

Donal:

Great! I had seen many reports about memory usage, some in the use of
markers, and had not had time to look into it. I will open a ticket and
make sure that this gets looked at for addition in 2.4. If you want to
create a patch of your differences between SVN and your changed version,
and add it to:
 
  http://trac.openlayers.org/ticket/369

that would be great.

(This is probably having a huge affect on some of the applications that
I am personally interested in, so I'm looking forward to getting this
fix in.)

Regards,
--
Christopher Schmidt
MetaCarta
_______________________________________________
Users mailing list
Users@...
http://openlayers.org/mailman/listinfo/users
Donal

Re: Markers' events memory leak?

Reply Threaded MoreMore options
Print post
Permalink
How can I add a patch to an existing ticket? Or should I open a new ticket?

Christopher Schmidt-4 wrote:

Donal:

Great! I had seen many reports about memory usage, some in the use of
markers, and had not had time to look into it. I will open a ticket and
make sure that this gets looked at for addition in 2.4. If you want to
create a patch of your differences between SVN and your changed version,
and add it to:
 
  http://trac.openlayers.org/ticket/369

that would be great.
Christopher Schmidt-2

Re: Markers' events memory leak?

Reply Threaded MoreMore options
Print post
Permalink
On Sun, Feb 18, 2007 at 11:55:41PM -0800, Donal wrote:
>
> How can I add a patch to an existing ticket? Or should I open a new ticket?

If you login as username 'openlayers', password 'openlayers':
 
  http://trac.openlayers.org/login

There should be an 'Attach file' button on
http://trac.openlayers.org/ticket/369 

Regards,
--
Christopher Schmidt
MetaCarta
_______________________________________________
Users mailing list
Users@...
http://openlayers.org/mailman/listinfo/users