Here's an obscure bug:
So, I've got a C++ program, which calls a Fortran subroutine. I can call the subroutine one of two ways:
Code:
auto future1 = std::async([&]{return foo_(filename, elements, vertices, &nV, &nE);});
future1.get();
or
Code:
foo_(filename, elements, vertices, &nV, &nE);
where
Code:
extern "C" {
void foo_(const char *filename, GLuint *elements, GLfloat *vertices, int *nV, int *nE);
};
appears in the C++ code, and
Code:
SUBROUTINE foo(mainfile, elements, vertices, nV, nE)
USE, INTRINSIC :: iso_c_binding, ONLY : c_float,c_int,c_char,c_null_char
IMPLICIT NONE
CHARACTER(c_char), INTENT(IN), DIMENSION(4096) :: mainfile
integer(c_int), INTENT(INOUT) :: nV
integer(c_int), INTENT(INOUT) :: nE
real(c_float), INTENT(INOUT) :: vertices(*)
integer(c_int), INTENT(INOUT) :: elements(*)
END SUBROUTINE foo
appears in a module to call the Fortran.
Funny thing is, usually either call from C++ works perfectly. The Fortran code opens a file (name passed by the C++) reads some data from it and stores it eventually in the arrays passed back to the C++. The error does not result from any manipulation of the passed C++ arrays or the filename, but occurs in an allocatable array in Fortran. The passed arrays are not allocated by Fortran; that is not allowed AFAIK.
However I noticed that one particular file read by the fortran code resulted in a fortran error while processing the file only if the fortran code was called directly (not async) from C++.
I also have two different versions of this C++ code, one written with wxWidgets, and one written with Qt. Only the Qt version triggers the runtime fault in the Fortran code with the one particular input file without the async call... and the issue does NOT occur running the same code in gdb. [EDIT: though this isn't consistent!]
I figure it has to be a memory allocation thing, where the Fortran code is reading a file of a certain size and for some reason doesn't get enough when it asks to allocate memory under this one weird circumstance, only with Qt, and no debugging, and no async call, and this one data file...
Any suggestions on where to start? I'm going to quiz the allocate() call in Fortran next, but it isn't a runtime error in the sense that the program crashes, it is just that a data sanity check that I wrote to check that the data is consistent fails. So perhaps Allocate() in Fortran thinks everything is hunky dory. [EDIT: indeed, allocate() reports no error, and allocated() confirms all arrays are allocated.]