Nasty corner case behaviour

H. S. Teoh hsteoh at
Thu Apr 18 05:01:17 UTC 2019

Got bitten by this today:

	import std.algorithm : filter, map;
	import std.stdio;
	struct S {
		bool flag;
		auto method() {
			return [ 1 ].filter!(e => flag);
	void main() {
		auto s = S(true);
		auto arr = [ s ];
		auto mappedArray =!(e => e.method);

Expected output:


Actual output:


Exercise for the reader: explain the problem.

Hint: it's not a bug in Phobos.

If method() is changed to the following, the problem goes away:

	auto method() {
		auto _flag = flag;
		return [ 1 ].filter!(e => _flag);

It should be obvious by now that the problem is caused by the lambda
incorrectly closing over a member variable in a struct instance that
subsequently goes out of scope, while the returned closure lives on.
Unfortunately, neither -dip25 nor -dip1000 catches this leakage, even if
main() is marked @safe.


Тише едешь, дальше будешь.

More information about the Digitalmars-d mailing list