Question Stopping JavaFx Canvas threads on script stop

Discussion in 'Developer Support' started by JJordan2, Feb 2, 2017.

  1. JJordan2

    Joined:
    Aug 19, 2016
    Messages:
    11
    Likes Received:
    3
    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.
     
    #1 JJordan2, Feb 2, 2017
    Last edited: Feb 2, 2017
  2. Savior

    Savior Java Warlord

    Joined:
    Nov 17, 2014
    Messages:
    4,909
    Likes Received:
    2,752
    How are you running those listeners in separate threads?
     
  3. JJordan2

    Joined:
    Aug 19, 2016
    Messages:
    11
    Likes Received:
    3
    Code (Text):
    1.         this.canvas.setOnMouseReleased(event -> {
    2.             System.out.println("RELEASED");
    3.          
    4.         });
    etc.
     
  4. Savior

    Savior Java Warlord

    Joined:
    Nov 17, 2014
    Messages:
    4,909
    Likes Received:
    2,752
    Th
    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?
     
  5. Party

    Party Client Developer

    Joined:
    Oct 12, 2015
    Messages:
    3,542
    Likes Received:
    1,572
    Some actual code might help.
     
  6. JJordan2

    Joined:
    Aug 19, 2016
    Messages:
    11
    Likes Received:
    3
    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.

    Not sure what else I can add

    FxController
    Code (Text):
    1.     @FXML
    2.     private Canvas canvas;
    3.  
    4.     private MapRenderer mapRenderer;
    5.  
    6.     @Override
    7.     public void initialize(URL location, ResourceBundle resources) {
    8.         try {
    9.             this.mapRenderer = new MapRenderer(this.canvas, new GameMap(
    10.                     "resources/smolmap.png", 0, 1391, 1647, 3));
    11.         } catch (IOException e) {
    12.             e.printStackTrace();
    13.         }
    14.     }
    Code (Text):
    1.     public void init() {
    2.         this.canvas.setOnMousePressed(e -> {
    3.             System.out.println("PRESSED");
    4.             if (e.getButton().equals(MouseButton.MIDDLE)) {
    5.                 this.isMoving = true;
    6.                 this.startMouseX = this.mapX;
    7.                 this.startMouseY = this.mapY;
    8.                 this.pressMouseX = e.getX();
    9.                 this.pressMouseY = e.getY();
    10.             }
    11.         });
    12.  
    13.         this.canvas.setOnMouseReleased(event -> {
    14.             System.out.println("RELEASED");
    15.             this.isMoving = false;
    16.         });
    17.  
    18.         this.canvas.setOnScroll(e -> {
    19.             System.out.println("SCROLL 1 " + this.scale);
    20.             final double oldWidth = this.mapX + (this.canvas.getWidth() / this.scale);
    21.             final double oldHeight = this.mapY + (this.canvas.getHeight() / this.scale);
    22.             this.scale += (e.getDeltaY() > 0 ? 1 : -1) * 0.25 * -1.0;
    23.             this.scale = Math.max(1.0, Math.min(8.0, this.scale));
    24.             final double newWidth = this.mapX + (this.canvas.getWidth() / this.scale);
    25.             final double newHeight = this.mapY + (this.canvas.getHeight() / this.scale);
    26.             this.mapX += Math.floor((oldWidth - newWidth) / 2.0);
    27.             this.mapY += Math.floor((oldHeight - newHeight) / 2.0);
    28.             this.update();
    29.             System.out.println("SCROLL 2 " + this.scale);
    30.         });
    31.  
    32.         this.canvas.setOnMouseDragged(e -> {
    33.             System.out.println("DRAGGED");
    34.             this.curX = e.getX();
    35.             this.curY = e.getY();
    36.             if (this.isMoving) {
    37.                 this.mapX = (this.startMouseX - (e.getX() - this.pressMouseX) / this.scale);
    38.                 this.mapY = (this.startMouseY - (e.getY() - this.pressMouseY) / this.scale);
    39.                 this.update();
    40.             }
    41.             this.update();
    42.         });
    43.  
    44.         this.canvas.setOnMouseMoved(e -> {
    45.             System.out.println("MOVED " + this.mapX + ", " + this.mapY + "{ " + this.scale + " }");
    46.             System.out.println("CANVAS " + this.canvas.getWidth() + ", " + this.canvas.getHeight());
    47.             this.curX = e.getX();
    48.             this.curY = e.getY();
    49.             this.update();
    50.         });
    51.     }
    bot start
    Code (Text):
    1.     @Override
    2.     public void onStart(String... args) {
    3.         this.controller.getMapRenderer().init();
    4.         this.controller.getMapRenderer().repaint();
    5.     }
     
  7. Party

    Party Client Developer

    Joined:
    Oct 12, 2015
    Messages:
    3,542
    Likes Received:
    1,572
    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.
     
  8. Savior

    Savior Java Warlord

    Joined:
    Nov 17, 2014
    Messages:
    4,909
    Likes Received:
    2,752
    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.
     

Share This Page

Loading...