Memory error detector MacOS

To find leaks and uninitialized values, I normally use Valgrind. But right now Valgrind does not work on on MacOS Big Sur or newer. My solution has been to boot up a Ubuntu VirtualBox occasionally, then build my code on there, then run with valgrind. This is annoying, and slow, and takes up too much ram on my little laptop.

I’m in search of either

  1. Lightweight fast virtual linux terminal, that takes little ram, which I can build and run my code on with valgrind
  2. Memory error detector that can work on MacOS that is as good as Valgrind.
1 Like

Are you looking for valgrind for C++ codes?

I very rarely needed to use Valgrind with Fortran code. Unless it calls into C.

I’m using Valgrind for Fortran. The feature I need most is detecting reading an uninitialized value.

3 Likes

Here is what I use with gfortran:

-Wall -Wextra -Wimplicit-interface -Wno-unused-function -fPIC -g \
-fcheck=all -fbacktrace -ffpe-trap=invalid,zero,overflow \
-finit-real=snan -finit-integer=-99999999

The -ffpe-trap=invalid,zero,overflow throws an exception if a nan is ever read from a variable (or perhaps only when it is assigned somewhere, which is almost the same thing), and -finit-real=snan initializes all real variables to nan. So this achieves the same thing, and with -fbacktrace you get a stacktrace to see where it happened.

Unfortunately it doesn’t work for integers, but there at least I initialize them to -finit-integer=-99999999, so the -fcheck=all triggers if they are used as array indices. And if they are used in some computation, I will usually get an obviously wrong answer, so it’s easier to spot.

2 Likes

If you can get access to the NAG compiler, I’ve had really good luck using the -C=undefined to find uses of uninitialized values.

2 Likes

Is NAG available for academic use for free?

@nicholaswogan , a lesson some teams have learned is to use Linux and/or Windows to debug memory errors in code and then instantiate the macOS version based on portable code. This is only going by the situations in the commercial space such as with Intel oneAPI where their tool Intel Inspector for memory leaks is available on Linux and Windows but not for macOS (likely the ValGrind issue lurking behind the scenes).

If you’re feeling brave, you can consider alpha Dr Memory.

Re: uninitialized variables, you can also try with Intel oneAPI the run-time option -check uninit for uninitialized variables e.g.,

   print *, x
end

C:\Temp>ifort /standard-semantics /check:uninit a.f90
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.3.0 Build 20210609_000000
Copyright (C) 1985-2021 Intel Corporation. All rights reserved.

Microsoft (R) Incremental Linker Version 14.29.30038.1
Copyright (C) Microsoft Corporation. All rights reserved.

-out:a.exe
-subsystem:console
a.obj

C:\Temp>a.exe
forrtl: severe (194): Run-Time Check Failure. The variable ‘_UNNAMED_MAIN$$$X’ is being used in ‘C:\Temp\a.f90(1,4)’ without being defined
Image PC Routine Line Source
a.exe 00007FF7DD9D5609 Unknown Unknown Unknown
a.exe 00007FF7DD9D104C Unknown Unknown Unknown
a.exe 00007FF7DDA20D7E Unknown Unknown Unknown
a.exe 00007FF7DDA21100 Unknown Unknown Unknown
KERNEL32.DLL 00007FFB1BBE7034 Unknown Unknown Unknown
ntdll.dll 00007FFB1D7C2651 Unknown Unknown Unknown

C:\Temp>

1 Like

Release 7.1 (available soon) will extend this feature to work with OpenMP codes.

Your academic institution may already have a licence. Academic licence of the compiler is offered at reduced cost. NAG is a not-for-profit enterprise.