# Segmentation fault when finalizing an inherited type with custom constructor

**URL:** <https://fortran-lang.discourse.group/t/segmentation-fault-when-finalizing-an-inherited-type-with-custom-constructor/7319>\
**Category:** GNU\
**Created:** [February 2, 2024, 8:04pm UTC](https://fortran-lang.discourse.group/t/segmentation-fault-when-finalizing-an-inherited-type-with-custom-constructor/7319 "2024-02-02T20:04:20Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhaiduce](https://avatars.discourse-cdn.com/v4/letter/j/ad7895/32.png) [@jhaiduce](https://fortran-lang.discourse.group/u/jhaiduce)\
**Post date:** [February 2, 2024, 8:04pm UTC](https://fortran-lang.discourse.group/t/segmentation-fault-when-finalizing-an-inherited-type-with-custom-constructor/7319/1 "2024-02-02T20:04:20Z")

</div>

I’m trying to troubleshoot some code that was working in earlier gfortran versions (5-12), but when compiled with gfortran 13.2.1 it produces a segmentation fault. The following appears to be the minimum that reproduces the problem:

```auto
module finalization_with_inheritance

  type::parent
     character(:), allocatable::name
  end type parent

  type, extends(parent)::child
   contains
     final::child_finalize
  end type child

  interface child
     module procedure new_child
  end interface child

contains

  type(child) function new_child(name)

    character(*)::name    

    new_child%name=name

  end function new_child

  subroutine child_finalize(this)

    type(child), intent(in)::this

  end subroutine child_finalize

end module finalization_with_inheritance

program main

  use finalization_with_inheritance, only: child

  implicit none

  type(child)::kid
  kid = child("Name")

end program main

```

Note that in the example the type `child` has the following properties:

- It inherits from another type (`parent`)
- It is instantiated by a call to a custom constructor that modifies a member of the ancestor type (`child%name`)
- It has a finalization routine

It appears that all of the above are required to duplicate the problem.

So my questions for the community are:

- Is there a problem with how I wrote my program? Or is this a compiler bug?
- If this is a compiler bug, is there any way to simplify the code and still reproduce the problem, and are there any workarounds I can try?

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [February 2, 2024, 8:48pm UTC](https://fortran-lang.discourse.group/t/segmentation-fault-when-finalizing-an-inherited-type-with-custom-constructor/7319/2 "2024-02-02T20:48:20Z")

</div>

I confirm it crashes most of the time with GFortran 13.2.0 under Ubuntu 23.10. Most of the time, because sometimes (15-20% of the time) it does not.

It runs with GFortran 11.4.0.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [February 2, 2024, 8:56pm UTC](https://fortran-lang.discourse.group/t/segmentation-fault-when-finalizing-an-inherited-type-with-custom-constructor/7319/3 "2024-02-02T20:56:22Z")

</div>

In FreeBSD 14.0 with GFortran 13.2.0:

```bash
$ gfortran -g bug.f90 && ./a.out

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0 0x825420379 in ???
#1 0x82541f4a5 in ???
#2 0x8216632d2 in ???
#3 0x823b01fa4 in atomic_load_p
	at /usr/src/contrib/jemalloc/include/jemalloc/internal/atomic.h:62
#4 0x823b01fa4 in rtree_leaf_elm_bits_read
	at /usr/src/contrib/jemalloc/include/jemalloc/internal/rtree.h:175
#5 0x823b01fa4 in rtree_szind_slab_read
	at /usr/src/contrib/jemalloc/include/jemalloc/internal/rtree.h:500
#6 0x823b01fa4 in ifree
	at /usr/obj/usr/src/amd64.amd64/lib/libc/jemalloc_jemalloc.c:2574
#7 0x823b01fa4 in __je_free_default
	at /usr/obj/usr/src/amd64.amd64/lib/libc/jemalloc_jemalloc.c:2775
#8 0x400f2e in __finalization_with_inheritance_MOD___ final_finalization_with_inheritance_Parent
	at /home/vmagnin/bug.f90:32
#9 0x400ac1 in __finalization_with_inheritance_MOD___ final_finalization_with_inheritance_Child
	at /home/vmagnin/bug.f90:32
#10 0x401214 in MAIN__
	at /home/vmagnin/bug.f90:41
#11 0x401253 in main
	at /home/vmagnin/bug.f90:36
Segmentation fault (core dumped)

```

It runs with GFortran 12.2.0.

---

<div class="post-metadata">

**Author:** ![blametroi](https://avatars.discourse-cdn.com/v4/letter/b/db5fbb/32.png) [@blametroi](https://fortran-lang.discourse.group/u/blametroi)\
**Post date:** [February 2, 2024, 11:01pm UTC](https://fortran-lang.discourse.group/t/segmentation-fault-when-finalizing-an-inherited-type-with-custom-constructor/7319/4 "2024-02-02T23:01:35Z")

</div>

Mac OS Arm same  
[crash.txt](https://fortran-lang.discourse.group/uploads/short-url/zgJRv3dAg0Lg6HtnqXnrwsQD550.txt) (758 Bytes)

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [February 2, 2024, 11:07pm UTC](https://fortran-lang.discourse.group/t/segmentation-fault-when-finalizing-an-inherited-type-with-custom-constructor/7319/5 "2024-02-02T23:07:59Z")

</div>

Since the bug has been confirmed, please consider filing a bug report through [GCC Bugzilla](https://gcc.gnu.org/bugzilla/). Or you can can send an email to

fortran at gcc dot gnu dot org

---

<div class="post-metadata">

**Author:** ![FedericoPerini](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/federicoperini/32/1750_2.png) [@FedericoPerini](https://fortran-lang.discourse.group/u/FedericoPerini)\
**Post date:** [February 3, 2024, 8:40am UTC](https://fortran-lang.discourse.group/t/segmentation-fault-when-finalizing-an-inherited-type-with-custom-constructor/7319/6 "2024-02-03T08:40:19Z")

</div>

Welcome to Discourse @jhaiduce!

I would suggest to file a bug to GCC Bugzilla.

In case it helps, please also include the following: the crash is caused by the inherited type not containing further variables. If you add even just one, then everything works again.

Try again with this version:

```fortran
  type, extends(parent)::child
     integer :: do_not_crash = 0
   contains
     final::child_finalize
  end type child

```

If this also works for you, please do submit this additional info to the gfortran team.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [February 3, 2024, 9:21am UTC](https://fortran-lang.discourse.group/t/segmentation-fault-when-finalizing-an-inherited-type-with-custom-constructor/7319/7 "2024-02-03T09:21:12Z")

</div>

According to @JerryD it may be related to:  
[https://gcc.gnu.org/bugzilla/show\_bug.cgi?id=110987](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110987)

As suggested by @Beliavsky and @FedericoPerini you could contact the GFortran team and post your minimalist code.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [February 3, 2024, 9:27am UTC](https://fortran-lang.discourse.group/t/segmentation-fault-when-finalizing-an-inherited-type-with-custom-constructor/7319/8 "2024-02-03T09:27:51Z")

</div>

Concerning GFortran _bug reporting / development / testing,_ I remind you that you can find up to date information here: [https://fortranwiki.org/fortran/show/GFortran#contributing](https://fortranwiki.org/fortran/show/GFortran#contributing)

---

<div class="post-metadata">

**Author:** ![jhaiduce](https://avatars.discourse-cdn.com/v4/letter/j/ad7895/32.png) [@jhaiduce](https://fortran-lang.discourse.group/u/jhaiduce)\
**Post date:** [February 3, 2024, 12:12pm UTC](https://fortran-lang.discourse.group/t/segmentation-fault-when-finalizing-an-inherited-type-with-custom-constructor/7319/9 "2024-02-03T12:12:45Z")

</div>

> [@FedericoPerini](#):
>
> In case it helps, please also include the following: the crash is caused by the inherited type not containing further variables. If you add even just one, then everything works again.

Thanks @FedericoPerini, that works for me too. I’ve also added variables to inherited types in my production code and that fixes the segmentation fault I encountered there.

---

<div class="post-metadata">

**Author:** ![jhaiduce](https://avatars.discourse-cdn.com/v4/letter/j/ad7895/32.png) [@jhaiduce](https://fortran-lang.discourse.group/u/jhaiduce)\
**Post date:** [February 3, 2024, 12:24pm UTC](https://fortran-lang.discourse.group/t/segmentation-fault-when-finalizing-an-inherited-type-with-custom-constructor/7319/10 "2024-02-03T12:24:59Z")

</div>

> [@vmagnin](#):
>
> According to @JerryD it may be related to:  
> [https://gcc.gnu.org/bugzilla/show\_bug.cgi?id=110987](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110987)

This looks to me like a duplicate of [https://gcc.gnu.org/bugzilla/show\_bug.cgi?id=110987](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110987). With that being the case, I’m wondering if it’s better _not_ to file a bug report. I’ll send a message to the gfortran mailing list and see what people there think about filing.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [February 3, 2024, 3:49pm UTC](https://fortran-lang.discourse.group/t/segmentation-fault-when-finalizing-an-inherited-type-with-custom-constructor/7319/11 "2024-02-03T15:49:15Z")

</div>

> [@jhaiduce](#):
>
> With that being the case, I’m wondering if it’s better _not_ to file a bug report. I’ll send a message to the gfortran mailing list and see what people there think about filing.

Yes, if you think it is that bug don’t fill a new report. But you can send a message on the mailing list or add a post to the bug 110987, with the shortest code that crashes and precise versions of your compilers and OS.

---

<div class="post-metadata">

**Author:** ![jhaiduce](https://avatars.discourse-cdn.com/v4/letter/j/ad7895/32.png) [@jhaiduce](https://fortran-lang.discourse.group/u/jhaiduce)\
**Post date:** [February 5, 2024, 4:05pm UTC](https://fortran-lang.discourse.group/t/segmentation-fault-when-finalizing-an-inherited-type-with-custom-constructor/7319/12 "2024-02-05T16:05:13Z")

</div>

I posted to the mailing list and the feedback there was also a suggestion to post a comment to bug 110987. I’ve added my code along with a summary of this thread to the bug report (see comments [5](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110987#c5) and [6](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110987#c6) on that bug). Thanks all for confirming this bug and helping me with reporting and troubleshooting!

---

<div class="post-metadata">

**Author:** ![JerryD](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jerryd/32/393_2.png) [@JerryD](https://fortran-lang.discourse.group/u/JerryD)\
**Post date:** [May 4, 2024, 7:34pm UTC](https://fortran-lang.discourse.group/t/segmentation-fault-when-finalizing-an-inherited-type-with-custom-constructor/7319/13 "2024-05-04T19:34:10Z")

</div>

Fix on March 29 on gcc 14.
