scope(exit) and destructor prioity

Jerry hurricane at hereiam.com
Mon Sep 18 20:30:20 UTC 2017


On Monday, 18 September 2017 at 20:26:05 UTC, Sasszem wrote:
> I'm currently working on a project and for that I've created a 
> thin OO-wrapper on top of derelict-sdl. However, when I close 
> my app, the program terminates with a segfault. I've managed to 
> track down the source, and found that the destructors of my 
> objects are called AFTER the scope(exit) statements. This 
> causes my program to call TTF_CloseFont() after TTF_Quit(), 
> resulting in a segfault.
> My questions:
>  - Can I force D to call the destructors BEFORE the scope(exit) 
> statements?
>  - If not, is there a way around?

It's called inbetween the destructors of wherever you put the 
scope(exit).

import std.stdio;

struct De
{
	~this() { writeln("De"); }
}

void main()
{
	De a;
	scope(exit) writeln("scope exit");
	De b;
}


Output:
De
scope exit
De


More information about the Digitalmars-d-learn mailing list