How to clean up client side resource

Hello.
I have trouble with clean uping resources on client side. It starts with
digitalclock
. But I offer easier example:
For example we have timer (com.google.gwt.user.client.Timer) on client side connector, which invoke server method through serverRpc:

public class MyComponent extends com.vaadin.ui.AbstractComponent {

	private MyComponentServerRpc rpc = new MyComponentServerRpc() {
		@Override
		public void doSmth() {
			System.out.println("This is Sparta!");
		}
	};
	
    public MyComponent() {
    	registerRpc(rpc);
    }

    @Override
    public MyComponentState getState() {
        return (MyComponentState) super.getState();
    }
    
    public void stopThis(){
    	getRpcProxy(MyComponentClientRpc.class).stopThis();
    }
}

public interface MyComponentClientRpc extends ClientRpc {

    public void stopThis();

}

@Connect(MyComponent.class)
public class MyComponentConnector extends AbstractComponentConnector {

	MyComponentServerRpc rpc = RpcProxy
			.create(MyComponentServerRpc.class, this);

	private Timer timer;
	private int i = 0;

	public MyComponentConnector() {
		registerRpc(MyComponentClientRpc.class, new MyComponentClientRpc() {
			@Override
			public void stopThis() {
				if (timer != null) {
					timer.cancel();
					timer = null;
					Window.alert("timer is stopped");
				} else {
					Window.alert("Hey! timer is already NULL");
				}
			}
		});

		timer = new Timer() {
			@Override
			public void run() {
				rpc.doSmth();
				i++;
				getWidget().setText("Hello " + i + " times!");
			}
		};
		timer.scheduleRepeating(5000);
	}

	@Override
	protected Widget createWidget() {
		return GWT.create(MyComponentWidget.class);
	}

	@Override
	public MyComponentWidget getWidget() {
		return (MyComponentWidget) super.getWidget();
	}

	@Override
	public MyComponentState getState() {
		return (MyComponentState) super.getState();
	}

	@Override
	public void onStateChanged(StateChangeEvent stateChangeEvent) {
		super.onStateChanged(stateChangeEvent);
	}
}

Widget is just Label for setting text.
Ok. I try to use this in my test UI class, which contents panel for mycomponent and two buttons: 1) stop clock and change content of the panel for new mycomponent. 2) just stop clock:

public class TestprojectdocUI extends UI {

	private VerticalLayout mainLayout;
	private MyComponent myComp;

	@Override
	protected void init(VaadinRequest request) {
		mainLayout = new VerticalLayout();
		setContent(mainLayout);
		final Panel panel = new Panel();
		myComp = new MyComponent();
		panel.setContent(myComp);
		mainLayout.addComponent(panel);
		Button again = new Button("again");
		again.addClickListener(new ClickListener() {
			@Override
			public void buttonClick(ClickEvent event) {
				myComp.stopThis();
				myComp = new MyComponent();
				panel.setContent(myComp);
			}
		});
		mainLayout.addComponent(again);

		Button stop = new Button("just stop!");
		stop.addClickListener(new ClickListener() {
			@Override
			public void buttonClick(ClickEvent event) {
				myComp.stopThis();
			}
		});
		mainLayout.addComponent(stop);

Ok. If i click Button for only stopping clock i don’t have problem.
But if i click Button for stopping clock and set new content. Clock continue stay on client side and send requests to server-side and we have:

WARNING: RPC call to ***.MyComponentServerRpc.doSmth received for connector 14 but no such connector could be found. Resynchronizing client.

I tried to stop this throught the State, but it also doesn’t work.
I don’t know why and what I can do with resource on client side. Please help.
Thank you.

You need to add a onUnregister method to your connector class and cancel the timer in there. Something like the Vaadin ProgressIndicator does in it’s
connector
.

Thank you, Jonni. Now it’s working.

hm…but why didn’t my way work?..

The clientside stopThis() is never called. No connector method calls are transferred to the client side when a component has been detached on the server side during the same server round trip.