LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-28-2022, 11:24 AM   #1
mostlyharmless
Senior Member
 
Registered: Jan 2008
Distribution: Arch/Manjaro, might try Slackware again
Posts: 1,851
Blog Entries: 14

Rep: Reputation: 284Reputation: 284Reputation: 284
?memory allocation differences with async calls?


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.]

Last edited by mostlyharmless; 11-29-2022 at 09:51 AM. Reason: update
 
Old 11-29-2022, 12:36 PM   #2
mostlyharmless
Senior Member
 
Registered: Jan 2008
Distribution: Arch/Manjaro, might try Slackware again
Posts: 1,851

Original Poster
Blog Entries: 14

Rep: Reputation: 284Reputation: 284Reputation: 284
Well, I solved the error but don't have an answer.

Turns out when I read the file, I didn't read one variable, which made no difference in some ways because it was filler in a non data area. However I used the value to verify the data count, and upon not reading it, the compiler is apparently free to do whatever it wants. Usually the value is initialized as zero, but apparently under the error condition sometimes random values in memory would end up in the variable, which could be interpreted as NaN, or whatever, which violated the assumptions of the data count.

So, a familiar problem in some ways: just the method of discovering it was different. It always helps to check data I/O, which I didn't do carefully enough, and it's always good to put sanity checking throughout your code, which helped me track this thing down very quickly, relatively speaking. Marking as SOLVED.
 
1 members found this post helpful.
Old 11-30-2022, 04:29 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Valgrind might have some ideas.
 
1 members found this post helpful.
Old 11-30-2022, 10:27 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
"Cross-language" projects are always tricky.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Get started using treq to make async calls in Python LXer Syndicated Linux News 0 03-18-2020 04:21 AM
how does java calls the system calls which are written in c babu198649 Linux - General 3 12-05-2011 03:40 AM
Application Virtual address space memory allocation - memory does not get free chamara82 Linux - General 4 01-01-2011 08:19 PM
a script calls another script which calls "bash" inside lordofring Linux - Software 10 03-18-2010 09:59 AM
memory allocation, application<-->driver charlies Programming 2 03-03-2001 11:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration