[Issue 22099] New: scope(exit) / finally blocks not are always executed inside of a anonymous function

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Jul 4 08:39:51 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=22099

          Issue ID: 22099
           Summary: scope(exit) / finally blocks not are always executed
                    inside of a anonymous function
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: luis.panadero at gmail.com

Trying this code with dmd v2.097.0 (on Ubuntu x86_64 20.04 LTS)
Giving this code : 

#!/usr/bin/env dub
import core.exception;
import core.stdc.stdio;
import core.stdc.stdlib;
import std.stdio : writeln;


void main()  {

  auto f = () {
    int* ptr = cast(int*) malloc(int.sizeof * 1000);
    try {
      fprintf(stderr, "Hello\n");
      throw new RangeError("bla bla");
    } finally {
      // writeln("Bye 1");
      fprintf(stderr, "Bye\n");
      free(ptr);
    }
  };

  try {
    f();
  } catch (RangeError ex) {
  }
}

Outputs only "Hello" on console. And running with valgrind, reveals a memory
leak as free(ptr) it's never called.

==22254== 4,000 bytes in 1 blocks are definitely lost in loss record 3 of 3
==22254==    at 0x483B7F3: malloc (in
/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==22254==    by 0x14B701: _D1f4mainFZ9__lambda1FNbZv (f.d:15)
==22254==    by 0x14B6C2: _Dmain (f.d:27)
==22254==    by 0x14C57A:
_D2rt6dmain212_d_run_main2UAAamPUQgZiZ6runAllMFZ9__lambda2MFZv (in
/tmp/.dub/build/f-~master/application-debug-linux.posix-x86_64-dmd_2097-FB7AFBA927D99FA3DDD7307BACA865DA/f)
==22254==    by 0x14C41C:
_D2rt6dmain212_d_run_main2UAAamPUQgZiZ7tryExecMFMDFZvZv (in
/tmp/.dub/build/f-~master/application-debug-linux.posix-x86_64-dmd_2097-FB7AFBA927D99FA3DDD7307BACA865DA/f)
==22254==    by 0x14C4F6: _D2rt6dmain212_d_run_main2UAAamPUQgZiZ6runAllMFZv (in
/tmp/.dub/build/f-~master/application-debug-linux.posix-x86_64-dmd_2097-FB7AFBA927D99FA3DDD7307BACA865DA/f)
==22254==    by 0x14C41C:
_D2rt6dmain212_d_run_main2UAAamPUQgZiZ7tryExecMFMDFZvZv (in
/tmp/.dub/build/f-~master/application-debug-linux.posix-x86_64-dmd_2097-FB7AFBA927D99FA3DDD7307BACA865DA/f)
==22254==    by 0x14C37D: _d_run_main2 (in
/tmp/.dub/build/f-~master/application-debug-linux.posix-x86_64-dmd_2097-FB7AFBA927D99FA3DDD7307BACA865DA/f)
==22254==    by 0x14C0D9: _d_run_main (in
/tmp/.dub/build/f-~master/application-debug-linux.posix-x86_64-dmd_2097-FB7AFBA927D99FA3DDD7307BACA865DA/f)
==22254==    by 0x14B77D: main (entrypoint.d:29)
==22254== 
==22254== LEAK SUMMARY:
==22254==    definitely lost: 4,000 bytes in 1 blocks
==22254==    indirectly lost: 0 bytes in 0 blocks
==22254==      possibly lost: 32 bytes in 1 blocks
==22254==    still reachable: 24 bytes in 1 blocks
==22254==         suppressed: 0 bytes in 0 blocks
==22254== Reachable blocks (those to which a pointer was found) are not shown.
==22254== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==22254== 
==22254== For lists of detected and suppressed errors, rerun with: -s
==22254== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)


Removing the comment of the writeln() , does the that the finally {} block
being executed as must be. Also, this happens with scope(exit).

With ldc2 1.20.1 (DMD v2.090.1, LLVM 10.0.0), the finally block it's always
executed.

--


More information about the Digitalmars-d-bugs mailing list