Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Does Component.detach() get invoked on Application close?
I have a Vaadin Component that registers a listener with some internal system resource inside the attach() method, and the de-registers the listener inside its detach() method.
If the Vaadin application somehow closes but this listener is never unregistered, then there will be a serious memory leak.
So I want to ensure that this doesn't happen.... which requires verifying the following:
- Application.close() is always invoked when the servlet session is closed or expires
- All Components are detach()'d whenever an Application is closed
Are both of these statements true? I'm pretty sure about the first, but not so sure about the second.
Thanks.
Hi,
Application.close() is always invoked when the servlet session is closed or expires
Yes
All Components are detach()'d whenever an Application is closed
No
The servlet container you are running the application in will take care of notifying Vaadin when the session expires or ends in another way and at that point Application.close() will be called. As you can see from the Application.close() method it does not do very much. If you want detach to be called or any other cleanup to be done, you should override close(). In other words, Component.detach() is called when the component is removed from the component hierarchy in the application, not when the whole application is closed.
Artur Signell: Hi,
Application.close() is always invoked when the servlet session is closed or expires
Yes
All Components are detach()'d whenever an Application is closed
No
The servlet container you are running the application in will take care of notifying Vaadin when the session expires or ends in another way and at that point Application.close() will be called. As you can see from the Application.close() method it does not do very much. If you want detach to be called or any other cleanup to be done, you should override close(). In other words, Component.detach() is called when the component is removed from the component hierarchy in the application, not when the whole application is closed.
Thanks Artur.