Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Question Stopping JavaFx Canvas threads on script stop

Joined
Aug 19, 2016
Messages
11
I currently have a JavaFX Canvas in my embeddable ui and a bunch of mouse/keyboard listeners (or whatever tf they are calling it in fx) being used with it. When I stop the bot these threads keep running, how do I stop this?

Also a side note, I don't think Garbage Collection/Clean up is working because when I stop a bot, If I have had images loaded in memory and I start and stop the bot enough times the heap starts to fill up. This could also be me doing something wrong.
 
Last edited:
Java Warlord
Joined
Nov 17, 2014
Messages
4,906
Th
Code:
        this.canvas.setOnMouseReleased(event -> {
            System.out.println("RELEASED");
         
        });

etc.
Thats not a thread, its a basic event listener.
Have you debugged to see that the instances of your ui related objects (panes, listeners, pictures, etc) definitely stay in the memory instead of getting garbage collected? Or how did you come to the conclusion?
 
Joined
Aug 19, 2016
Messages
11
Th

Thats not a thread, its a basic event listener.
Are you aware of how the observer pattern works?
On that note, resource monitor shows normal thread creation and destruction after further investigation and the thread is destroyed after closing the interface.

Th
Have you debugged to see that the instances of your ui related objects (panes, listeners, pictures, etc) definitely stay in the memory instead of getting garbage collected? Or how did you come to the conclusion?
Yes I have debugged it, if i set say, my image object to null my console gets flooded with NPEs
I can see it my resource monitor that ram is not being thread when a bot ends, after starting and stopping a bot around 10 times there's an extra 200mb of ram being used that does not go away until I get a heap exception.

Some actual code might help.

Not sure what else I can add

FxController
Code:
    @FXML
    private Canvas canvas;

    private MapRenderer mapRenderer;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        try {
            this.mapRenderer = new MapRenderer(this.canvas, new GameMap(
                    "resources/smolmap.png", 0, 1391, 1647, 3));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Code:
    public void init() {
        this.canvas.setOnMousePressed(e -> {
            System.out.println("PRESSED");
            if (e.getButton().equals(MouseButton.MIDDLE)) {
                this.isMoving = true;
                this.startMouseX = this.mapX;
                this.startMouseY = this.mapY;
                this.pressMouseX = e.getX();
                this.pressMouseY = e.getY();
            }
        });

        this.canvas.setOnMouseReleased(event -> {
            System.out.println("RELEASED");
            this.isMoving = false;
        });

        this.canvas.setOnScroll(e -> {
            System.out.println("SCROLL 1 " + this.scale);
            final double oldWidth = this.mapX + (this.canvas.getWidth() / this.scale);
            final double oldHeight = this.mapY + (this.canvas.getHeight() / this.scale);
            this.scale += (e.getDeltaY() > 0 ? 1 : -1) * 0.25 * -1.0;
            this.scale = Math.max(1.0, Math.min(8.0, this.scale));
            final double newWidth = this.mapX + (this.canvas.getWidth() / this.scale);
            final double newHeight = this.mapY + (this.canvas.getHeight() / this.scale);
            this.mapX += Math.floor((oldWidth - newWidth) / 2.0);
            this.mapY += Math.floor((oldHeight - newHeight) / 2.0);
            this.update();
            System.out.println("SCROLL 2 " + this.scale);
        });

        this.canvas.setOnMouseDragged(e -> {
            System.out.println("DRAGGED");
            this.curX = e.getX();
            this.curY = e.getY();
            if (this.isMoving) {
                this.mapX = (this.startMouseX - (e.getX() - this.pressMouseX) / this.scale);
                this.mapY = (this.startMouseY - (e.getY() - this.pressMouseY) / this.scale);
                this.update();
            }
            this.update();
        });

        this.canvas.setOnMouseMoved(e -> {
            System.out.println("MOVED " + this.mapX + ", " + this.mapY + "{ " + this.scale + " }");
            System.out.println("CANVAS " + this.canvas.getWidth() + ", " + this.canvas.getHeight());
            this.curX = e.getX();
            this.curY = e.getY();
            this.update();
        });
    }

bot start
Code:
    @Override
    public void onStart(String... args) {
        this.controller.getMapRenderer().init();
        this.controller.getMapRenderer().repaint();
    }
 
Client Developer
Joined
Oct 12, 2015
Messages
3,760
Bindings aren't removed until the cavas is unloaded, for a start (which doesn't happen until the reference to the EmbeddableUI is lost - stopping the bot and navigating away from the loaded screen). For memory cleanup you could call Runtime.gc() to clean up onStop(), though there's a potential there to just remove all of the WeakReferences holding game information in any other active instances.
 
Java Warlord
Joined
Nov 17, 2014
Messages
4,906
Are you aware of how the observer pattern works?
On that note, resource monitor shows normal thread creation and destruction after further investigation and the thread is destroyed after closing the interface.


Yes I have debugged it, if i set say, my image object to null my console gets flooded with NPEs
I can see it my resource monitor that ram is not being thread when a bot ends, after starting and stopping a bot around 10 times there's an extra 200mb of ram being used that does not go away until I get a heap exception.
Are you saying that a new thread is created for each event listener you add? Because I must admit that I don't know exactly how javafx implemented the observer pattern.
And as party said, once the bot is stopped AND you clicked away from the remaining element, it should be freed. Else there must be a thread you created which references your UI and didnt end upon stopping the bot.
 
Top