Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28499 Discussions

Debug possible memory corruption

Amorin
Novice
1,005 Views

Hi,

 

I have a crash during deallocation of a derived type. I found a thread that may describe a relevant debugging step for me:

community.intel.com/t5/Intel-Fortran-Compiler/heap-corruption-error/m-p/890765#M78197

 

My question is: how to do the same (find the hidden allocation header) for a derived type instead of an array? For the array it was done by associating a pointer to just after the last element.

 

[edit to make a proper link]

0 Kudos
4 Replies
jimdempseyatthecove
Honored Contributor III
985 Views

LOC(yourUDT) will give you the address of the data portion of the UDT. Preceding this will be one or more pointer sized elements for the allocation header. What is present, depends on the heap manager. There is at least one for a link, potentially a second for count (in case of array), potentially a third for padding (in case of aligned allocation), possibly a fourth for debugging, ...

You may have to experiment to figure out the header.

Also, the heap manager (unless the TBB scalable allocator is used) is the CRT library heap. You can link in the debug version of the heap and then use the C heap diagnostic functions. See:

https://learn.microsoft.com/en-us/cpp/c-runtime-library/find-memory-leaks-using-the-crt-library?view=msvc-170

https://learn.microsoft.com/en-us/cpp/c-runtime-library/crt-debug-heap-details?view=msvc-170

https://valgrind.org/

https://dmalloc.com/      (I havn't seen usages of this)

https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/heapchk?view=msvc-170

https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/heapwalk?view=msvc-170

 

Jim Dempsey

 

0 Kudos
Amorin
Novice
919 Views

Thanks.

I was able to print out the integer values of the allocation header elements by defining the variables

INTEGER(KIND=INT_PTR_KIND()) :: iAllocHeader
INTEGER(KIND=INT_PTR_KIND()) :: allocHeader
POINTER (iAllocHeader, allocHeader)

Then print out values for n = 1,2,... by

iAllocHeader = LOC(myObject) - INT_PTR_KIND() * n
write(*,*) "Allocation header",allocHeader

 I could see that the value for n=3 was changing slightly at some point, but other objects that deallocate without problem also had a change on this on. Other values were not changing.

I'll probably have to look at your links to check the heap more thoroughly.

0 Kudos
jimdempseyatthecove
Honored Contributor III
981 Views

Also, make sure you do a Debug build with all build and runtime diagnostics enabled.

Jim Dempsey

0 Kudos
Amorin
Novice
918 Views

I have done that too, also with another compiler which is generally very good at catching memory issues, but it works fine in the debug build

0 Kudos
Reply