Memory leak with DOM method ?

11 views
Skip to first unread message

step...@yahoo.fr

unread,
Oct 6, 2008, 11:09:09 AM10/6/08
to Google Web Toolkit
Hello,

Is there problem of memory leak when using DOM classes and not
widget ?

For example when i use DOM.seteventlistener and not
button.addclicklistener ?

thanks for your answer !

Steph

Reinier Zwitserloot

unread,
Oct 6, 2008, 4:02:06 PM10/6/08
to Google Web Toolkit
Yes. Don't use DOM.* unless you really know what you are doing.

step...@yahoo.fr

unread,
Oct 7, 2008, 9:38:08 AM10/7/08
to Google Web Toolkit
Thanks.

Is there recommandation to use DOM ? how must i use
DOM.seteventlistener ?

Is it possible to have memory leak when using DOM.getElementAttribute
or DOM.getParent for example ?

Thanks

Thomas Broyer

unread,
Oct 7, 2008, 10:29:20 AM10/7/08
to Google Web Toolkit


On 7 oct, 15:38, "step3...@yahoo.fr" <step3...@yahoo.fr> wrote:
> Thanks.
>
> Is there recommandation to use DOM ? how must i use
> DOM.seteventlistener ?

You have to call DOM.setEventListener(elt, null) whenever "elt" is
detached from the document (that's what Widget.onDetach does) or at
least at "unload" time (RootPanels are detached at on unload, and thus
detach their children in cascade; same goes for widgets created with
their static wrap() method, as they automatically register themselves
for RootPanel.detachOnWindowClose).

Actually, you'd rather create a Widget and follow the static
wrap(Element) pattern found on other widgets (Button, TextBox, etc.)
than directly play with DOM.setEventListener. Or just be careful to
reset event listeners to "null" before "unload" for each element
you've attached a listener to.

> Is it possible to have memory leak when using DOM.getElementAttribute
> or DOM.getParent for example ?

I don't think so. Actually, memory leaks are mostly caused by DOM
elements referencing javascript objects referencing back the DOM
element (which is easily done when attaching events in "pure
JavaScript"). You shouldn't suffer from memory leaks with GWT except
if you start playing with DOM.setEventListener and/or JSNI.

That being said, I'm not a JavaScript expert wrt memory leaks, so
you'd better check before taking my word for it :-P

jay

unread,
Oct 7, 2008, 4:24:34 PM10/7/08
to Google Web Toolkit
In a few cases, I've found that the Widget I'm working with isn't
getting events as I expected. Fixing this entails something like this:
class MyWidget extends Widget {
. . .
protected void onAttach() {
super.onAttach();
DOM.setEventListener( getElement(), this );
}

Can someone tell me if I'm at risk of a memory leak by doing this?

(For completeness, one place I had to do this was in a class I
created, ResizablePanel, which captures mouse events to allow the user
to click & drag an edge of the panel to resize it. [You'd never guess
that functionality from the class name, huh? :-) I don't remember now
where the mouse events were going, but the only way I could figure out
for this class to get them was to set itself as the event listener...]

Thanks,

jay
Reply all
Reply to author
Forward
0 new messages