Druid: Stack Overflow

Created on 7 Sep 2020  路  6Comments  路  Source: linebender/druid

Hey friends,

I've been trying to learn Druid and attempted to explore how to effectively structure the application. I finally got it compiling, but it immediately stack overflows.

Minimal Example: https://github.com/Deedasmi/druid_overflow
OS: macOS 10.15.6

What I expected to happen (may be way off, still learning Druid):

GameState initializes with next_scene GameScene::Initial. InitialState also initializes with a default next_scene of GameScene::Initial. Druid begins checking (via ViewSwitcher) whether GameState.get_next_scene() is NOT GameScene::Initial and updates the ViewSwitcher accordingly. However, in this example, nothing ever mutates GameState.current_scene, and I would expect the application to load on "Initial" and break if you clicked any buttons.

What actually happens:
Application stack overflows without ever rendering a window.

Notes:
I realize this structure is flawed as I can't see a way to mutate GameState.current_scene in the context of the view switcher, but I still can't see any reason it would stack overflow, so I figured it was worth a report.

question

Most helpful comment

No this is not ViewSwitchers fault. The problem is this:

impl Default for LoginState {
    fn default() -> Self {
        LoginState {
            next_scene: GameScene::Login,
            .. Default::default()
        }
    }
}

Calling default recursively.

All 6 comments

I'm really not sure what's going on here, but my first suspicion would be that there's a recursive data structure somewhere? I don't _think_ this should be in druid code, although skimming your example I don't see anything obvious.

ping @Finnerale, It sounds similar why you decided close #751

Maybe the problem with the ViewSwitcher:

https://github.com/Deedasmi/druid_overflow/blob/7c86bf41e3c8cee8993f8b0356c7e10ea444c67d/src/game.rs#L37

No this is not ViewSwitchers fault. The problem is this:

impl Default for LoginState {
    fn default() -> Self {
        LoginState {
            next_scene: GameScene::Login,
            .. Default::default()
        }
    }
}

Calling default recursively.

Huh, apparently I don't know exactly how .. Default::default() works. Thought it just called default on all currently undefined fields, which had no recursion. That did fix it though. Thanks!

@Deedasmi Well basically

impl Default for LoginState {
    fn default() -> Self {
        LoginState {
            next_scene: GameScene::Login,
            .. Default::default()
        }
    }
}

is the same as

impl Default for LoginState {
    fn default() -> Self {
        LoginState {
            next_scene: GameScene::Login,
            .. LoginState::default()
        }
    }
}

where all missing fields (e.g. all except next_scene) will be taken from LoginState::default().

I hope that makes it a bit clearer.

Oh, so it instantiates and moves. I assumed it would enumerate identities. That does help, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chris-zen picture chris-zen  路  6Comments

MGlolenstine picture MGlolenstine  路  3Comments

cmyr picture cmyr  路  3Comments

raphlinus picture raphlinus  路  4Comments

mrandri19 picture mrandri19  路  4Comments