Say I have a basic WinRT class:
// midl
runtimeclass Class: IClosable
{
Class();
}
// .h
struct Class : ClassT<Class>
{
Class() = default;
void Close();
~Class();
private:
unique_ptr<Helper> helper = make_unique<Helper>();
};
Now it all works correctly, but when calling my class from C# within a using
block (which then calls Dispose
, which is translated to Close
), the destructor of my class never gets called (maybe when the GC triggers?), which means that my helper never gets destroyed. This is critical for me because that’s what flushes the IO streams and closes the files, until then I have 0 byte files sitting around.
As a work-around I made the destructor call Close()
(to funnel the code through one function), then made Close()
reset the smart pointer to destroy it, but this is so anti-C++. Is there a better, more idiomatic way of doing this that I’m missing?