0

This is one of those weird questions, I'm afraid, sorry :(

I have a piece of code that uses function pointers. It's worked fine for me until I tried using it on a Ubuntu 18 kernel installed on a Windows 10 machine. I wanted to compile it in debug mode using gfortran and hence used the -O0 flag. Then, I started getting inexplicable seg faults when trying to call a function to which a pointer is pointing:

program test01

implicit none

! ---
abstract interface
    real(8) function funcInterface(t)
        real(8), intent(in) :: t
    end function funcInterface
end interface

procedure(funcInterface), pointer :: testPointer => null()

real(8) :: result

! ---
testPointer => testFunction
result = testPointer(0.0d0) ! <--- issues here.

contains

real(8) function testFunction(t)
    real(8), intent(in) :: t
    testFunction = -1.0d0 * t
end function testFunction

end program test01

Here's a trace using the GNU debugger:

17          testPointer => testFunction
(gdb) print(testPointer)
$1 = (PTR TO -> ( real(kind=8) ()())) 0x0
(gdb) next
18          result = testPointer(0.0d0)
(gdb) print(testPointer)
$2 = (PTR TO -> ( real(kind=8) ()())) 0x7ffffffed2a0
(gdb) next

Program received signal SIGSEGV, Segmentation fault.
0x00007ffffffed2a0 in ?? ()

I also checked it with valgrind and it came out clear:

==5436== HEAP SUMMARY:
==5436==     in use at exit: 0 bytes in 0 blocks
==5436==   total heap usage: 21 allocs, 21 frees, 13,520 bytes allocated
==5436==
==5436== All heap blocks were freed -- no leaks are possible

When I compiled the exact same code on a different Linux machine, it worked just fine, both using gfortran and ifort with the same equivalent flags. On the machine with problems, I've tried gfortran versions 5 through 8, to the same effect. I suppose I can just not disable the optimisation and get on with the project, but it gets in the way of my debugging. If someone knows just why this causes issues on one machine but not the other, I'd really like to know.

Thanks,

Artur

Artur
  • 407
  • 2
  • 8
  • 1
    Note that 1d0 is not always compatible with real(8). You should make a kind constant and use it or just use double precision. It is not the reason of the crash though. – Vladimir F Героям слава Aug 16 '19 at 15:41
  • Thanks Vladimir, good point. – Artur Aug 16 '19 at 16:22
  • Works for me on Linux Mint 19 with gfortran 5.5, gfortran 6.5, gfortran 7.4 and gfortran 8.3. What version of gfortran are you using and exactly what flags are on the command line? – Ian Bush Aug 16 '19 at 16:47
  • I tried different gfortran versions but I mostly use this one: GNU Fortran (Ubuntu 5.5.0-12ubuntu1) 5.5.0 20171010 Here are the exact flags: Doesn't work: gfortran -g -Wall -O0 mwe.f90 -o mwe && ./mwe Works: gfortran -g -Wall -O1 mwe.f90 -o mwe && ./mwe – Artur Aug 16 '19 at 16:49
  • Sorry - exactly the same version of gfortran 5.5 but can't reproduce the error. For what it is worth uname -a gives Linux eris 4.15.0-54-generic #58-Ubuntu SMP Mon Jun 24 10:55:24 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux – Ian Bush Aug 16 '19 at 16:59
  • Mine is: Linux myPc 4.4.0-17763-Microsoft #253-Microsoft Mon Dec 31 17:49:00 PST 2018 x86_64 x86_64 x86_64 GNU/Linux – Artur Aug 16 '19 at 17:05
  • Works also for me with gfortran from 4.9 / 5.5 / 8.3 / 9.1 on Mac OSX10.11 – roygvib Aug 16 '19 at 18:31
  • 1
    Bit of a guess, but if you are using WSL on Windows 10, it may be related to this https://stackoverflow.com/q/50717926/3967096 – jbdv Aug 16 '19 at 20:48
  • 2
    Thanks, that does seem to be the case. If I follow the comments and referenced links correctly, what I'm trying to do won't work on the platform I've been trying it on. – Artur Aug 17 '19 at 09:00
  • 1
    Possible duplicate of [Segmentation fault when passing internal function as argument](https://stackoverflow.com/questions/49965980/segmentation-fault-when-passing-internal-function-as-argument) – jbdv Aug 17 '19 at 09:52

0 Answers0