Giter Club home page Giter Club logo

Comments (7)

sumibi-yakitori avatar sumibi-yakitori commented on June 11, 2024
Relm4:
use gtk::prelude::*;
use item::Item;
use relm4::{
  factory::{FactoryComponent, FactoryVecDeque, FactoryView},
  gtk,
  prelude::*,
  FactorySender,
};
use std::time::Duration;

fn main() {
  gtk::init().unwrap();
  let app = relm4::main_application();

  RelmApp::from_app(app).run::<List>(());
}

pub struct List {
  items: FactoryVecDeque<Item>,
}

#[derive(Debug)]
pub enum ListInput {
  Update,
}

impl SimpleComponent for List {
  type Init = ();
  type Input = ListInput;
  type Output = ();
  type Root = gtk::ApplicationWindow;
  type Widgets = ();

  fn init_root() -> Self::Root {
    gtk::ApplicationWindow::builder().build()
  }

  fn init(
    _init: Self::Init,
    root: Self::Root,
    sender: ComponentSender<Self>,
  ) -> ComponentParts<Self> {
    let list = gtk::Box::builder().build();
    root.set_child(Some(&{
      let container = gtk::ScrolledWindow::builder().build();
      container.set_child(Some(&list));
      container
    }));
    let items = FactoryVecDeque::builder().launch(list).detach();
    let model = Self { items };
    std::thread::spawn({
      let sender = sender.clone();
      move || loop {
        sender.input(ListInput::Update);
        std::thread::sleep(Duration::from_secs(1));
      }
    });
    ComponentParts { widgets: (), model }
  }

  fn update(&mut self, message: Self::Input, _sender: ComponentSender<Self>) {
    match message {
      ListInput::Update => {
        let mut guard = self.items.guard();
        guard.clear();
        for _ in 0..10000 {
          guard.push_back(Item {});
        }
      }
    }
  }
}

mod item {
  use super::*;

  #[derive(Clone)]
  pub struct Item;

  type RootWidget = gtk::Box;

  #[derive(Debug)]
  pub struct FactoryWidgets {}

  impl FactoryComponent for Item {
    type Init = Self;
    type Input = ();
    type Output = ();
    type CommandOutput = ();
    type ParentWidget = gtk::Box;
    type Root = RootWidget;
    type Widgets = FactoryWidgets;
    type Index = DynamicIndex;

    fn init_root(&self) -> Self::Root {
      RootWidget::builder()
        .hexpand(false)
        .vexpand(true)
        .halign(gtk::Align::Start)
        .build()
    }

    fn init_widgets(
      &mut self,
      _index: &DynamicIndex,
      _root_widget: Self::Root,
      _returned_widget: &<Self::ParentWidget as FactoryView>::ReturnedWidget,
      _sender: FactorySender<Self>,
    ) -> Self::Widgets {
      Self::Widgets {}
    }

    fn init_model(value: Self::Init, _index: &DynamicIndex, _sender: FactorySender<Self>) -> Self {
      value
    }
  }
}
GTK4:
use gtk::{prelude::*, Application, ApplicationWindow};
use relm4::{prelude::gtk, RelmRemoveAllExt};
use std::time::Duration;

fn main() {
  let application = Application::builder()
    .application_id("com.example.list")
    .build();

  application.connect_activate(|app| {
    let window = ApplicationWindow::builder()
      .application(app)
      .default_width(400)
      .default_height(400)
      .build();

    window.set_child(Some(&{
      let container = gtk::ScrolledWindow::builder().build();
      container.set_child(Some(&{
        let list = gtk::Box::builder().build();

        gtk::glib::timeout_add_local(Duration::from_secs(1), {
          let list = gtk::glib::clone::Downgrade::downgrade(&list);
          move || {
            let Some(list) = list.upgrade()
            else {
              return false.into();
            };

            list.remove_all();
            for _ in 0..10000 {
              list.append(&{ gtk::Box::builder().build() });
            }

            true.into()
          }
        });

        list
      }));
      container
    }));

    window.show();
  });

  application.run();
}

from relm4.

AaronErhardt avatar AaronErhardt commented on June 11, 2024

Thanks for the report. This is actually not quite unexpected because FactoryVecDeque is using a little bit of unsafe to make it possible to implement the Index trait. Maybe something in the logic is wrong so that the destructor isn't run which would free the allocation.

Since we just published version 0.7 and 0.8, would you mind checking whether the problem exists there as well?

from relm4.

sumibi-yakitori avatar sumibi-yakitori commented on June 11, 2024

@AaronErhardt
Thanks for the quick reply. This is the result of running on Relm 0.8.

I first found this issue with Relm 0.6 and was going to report the issue, but I just learned that Relm 0.7/0.8 has just been released (Congratulations!🎉)

So I upgraded my crate in hopes that this issue had been resolved, but alas, it was not.

from relm4.

AaronErhardt avatar AaronErhardt commented on June 11, 2024

The leak seems to be caused by gtk-rs-core and is simply triggered very often by Relm4s factories. Let's see what the gtk-rs devs say about this (you can follow the referenced issue).

from relm4.

sumibi-yakitori avatar sumibi-yakitori commented on June 11, 2024

Thank you!

from relm4.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.