I've been experiencing some odd behavior, where it would appear that a struct's destructor is being called before the object's lifetime expires. More likely I am misunderstanding something about the lifetime rules for structs. I haven't been able to reproduce with a particularly minimal example, so I will try to explain with my current code:

I have a struct called "TileView", with the relevant parts looking like so:
[code]
struct TileView
{
    this(Texture.Handle wallsTexture, Texture.Handle topTexture)
    {
// Work happens here, but it doesn't seem to matter to reproducing the condition
    }

    // Destructor added for debugging after seeing odd behavior
    ~this()
    {
        import std.stdio;
        writeln("HERE2");
    }
// ...more implementation that doesn't seem to affect the condition...
}
[/code]

An instance of this is stored in another struct called "View", with the relevant parts looking like so:
[code]
struct View
{
    this(/* irrelevant args here */)
    {
        writeln("HERE1a");
m_tileView = TileView(Texture.create(loadTGA(makeInputStream!FileInputStream("resources/images/grass-topped-clay.tga").handle)), Texture.create(loadTGA(makeInputStream!FileInputStream("resources/images/grass-outlined.tga").handle)));//, Texture.create(loadTGA(makeInputStream!FileInputStream("resources/images/grass-outlined.tga").handle)));
        writeln("HERE1b");
    }

    TileView m_tileView;
    // ...more irrelevant implementation...
}
[/code]

The output from the two writelns in View and the one in TileView is:
[output]
HERE1a
HERE2
HERE1b
[/output]

So the destructor of TileView is being called during its construction. Flow proceeds normally (e.g., no exception is thrown), as demonstrated by "HERE1b" being printed. Interestingly enough, it all seems to hinge on the second argument to TileView's constructor; if I make it on a separate line beforehand and pass it in, or if I don't pass in a second argument at all, I don't see this behavior. In fact, almost any attempt I've made to reduce the problem for illustration causes it to vanish, which is unfortunate.

From this non-reduced situation, does anything jump out? Am I missing something about struct lifetimes? This is the only place I instantiate a TileView.

Thanks!


Reply via email to