https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79053

            Bug ID: 79053
           Summary: Memory leak from array constructor + array index off
                    by one the array
           Product: gcc
           Version: 6.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pedsxing at gmx dot net
  Target Milestone: ---

Created attachment 40498
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=40498&action=edit
Source code that shows the problem

This is a weird one. It might actually be two bugs occuring in the same piece
of code.

I have a derived type (called string) with a type-bound method that accepts an
array of said derived type. Furthermore it has a user-defined constructor and a
deferred-length character member.

If I pass an array constructor with calls to the user-defined constructor into
the type-bound method, two strange things happen:

1. The array indices are off by one inside the type-bound method. In the
attached example, it prints "another longer string" and an empty line. The
first element "a string" is not printed. This problem can be fixed by declaring
"others" as "type(string)" instead of "class(string)"

2. Apparently the memory of the array is leaked. At least valgrind reports it
as definitely leaked:

==2730== HEAP SUMMARY:
==2730==     in use at exit: 29 bytes in 2 blocks
==2730==   total heap usage: 24 allocs, 22 frees, 11,914 bytes allocated
==2730== 
==2730== 8 bytes in 1 blocks are definitely lost in loss record 1 of 2
==2730==    at 0x4C2AB8D: malloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2730==    by 0x400F62: __stringmodule_MOD_newfromrawconstr (in
/home/robert/workspace/ftl/compilerbugs/bug)
==2730==    by 0x4010D9: leak_ (in /home/robert/workspace/ftl/compilerbugs/bug)
==2730==    by 0x4013A3: MAIN__ (in
/home/robert/workspace/ftl/compilerbugs/bug)
==2730==    by 0x4013DA: main (in /home/robert/workspace/ftl/compilerbugs/bug)
==2730== 
==2730== 21 bytes in 1 blocks are definitely lost in loss record 2 of 2
==2730==    at 0x4C2AB8D: malloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2730==    by 0x400F62: __stringmodule_MOD_newfromrawconstr (in
/home/robert/workspace/ftl/compilerbugs/bug)
==2730==    by 0x4010F6: leak_ (in /home/robert/workspace/ftl/compilerbugs/bug)
==2730==    by 0x4013A3: MAIN__ (in
/home/robert/workspace/ftl/compilerbugs/bug)
==2730==    by 0x4013DA: main (in /home/robert/workspace/ftl/compilerbugs/bug)
==2730== 
==2730== LEAK SUMMARY:
==2730==    definitely lost: 29 bytes in 2 blocks
==2730==    indirectly lost: 0 bytes in 0 blocks
==2730==      possibly lost: 0 bytes in 0 blocks
==2730==    still reachable: 0 bytes in 0 blocks
==2730==         suppressed: 0 bytes in 0 blocks

If I put the call that contains the array constructor into a loop, every
iteration leaks some memory.

Reply via email to