www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Access Violation when passing the result of a C function directly to a

reply Timothy Foster <timfost aol.com> writes:
I'm compiling on Windows 7 x64, DMD32 D Compiler v2.075.1 and I'm 
using Derelict Fmod to handle audio in my application. Every Fmod 
function returns an int telling me if the function ran okay, or 
if there was an error. I've written the following helper function 
that will print something for me if it encounters an error:

static void ErrorFMOD(int result, string msg = ""){
	if(result != FMOD_OK)
		writeln("[ FMOD ] ", msg, FMOD_ErrorString(result));
}

This function has been causing Access Violation Errors when I 
call it. It doesn't happen every time and it doesn't always 
happen in the same place. The errors occur _even when I comment 
out everything inside the function_. I've been calling it like so:

ErrorFMOD(FMOD_System_Create(&system), "Error Creating System: ");

Making the calls without my helper function doesn't cause an 
Access Violation.
Calling it like this is the only thing that seems to fix it:

auto result = FMOD_System_Create(&system);
ErrorFMOD(result, "Error Creating System: ");

Is this a known issue, or am I required to save the result of a C 
function to variable before passing it into another function or?
Sep 14
next sibling parent reply Vadim Lopatin <coolreader.org gmail.com> writes:
On Friday, 15 September 2017 at 04:01:13 UTC, Timothy Foster 
wrote:
 I'm compiling on Windows 7 x64, DMD32 D Compiler v2.075.1 and 
 I'm using Derelict Fmod to handle audio in my application. 
 Every Fmod function returns an int telling me if the function 
 ran okay, or if there was an error. I've written the following 
 helper function that will print something for me if it 
 encounters an error:

 static void ErrorFMOD(int result, string msg = ""){
 	if(result != FMOD_OK)
 		writeln("[ FMOD ] ", msg, FMOD_ErrorString(result));
 }

 This function has been causing Access Violation Errors when I 
 call it. It doesn't happen every time and it doesn't always 
 happen in the same place. The errors occur _even when I comment 
 out everything inside the function_. I've been calling it like 
 so:

 ErrorFMOD(FMOD_System_Create(&system), "Error Creating System: 
 ");

 Making the calls without my helper function doesn't cause an 
 Access Violation.
 Calling it like this is the only thing that seems to fix it:

 auto result = FMOD_System_Create(&system);
 ErrorFMOD(result, "Error Creating System: ");

 Is this a known issue, or am I required to save the result of a 
 C function to variable before passing it into another function 
 or?
Probably you have to use const char * msg when interfacing with C. string is a struct - size_t length and const char * value
Sep 15
parent Timothy Foster <timfost aol.com> writes:
On Friday, 15 September 2017 at 10:33:55 UTC, Vadim Lopatin wrote:
 On Friday, 15 September 2017 at 04:01:13 UTC, Timothy Foster 
 wrote:
 [...]
Probably you have to use const char * msg when interfacing with C. string is a struct - size_t length and const char * value
The string doesn't touch any C functions, it just gets printed in the writeln() call.
Sep 15
prev sibling next sibling parent reply Kagamin <spam here.lot> writes:
On Friday, 15 September 2017 at 04:01:13 UTC, Timothy Foster 
wrote:
 am I required to save the result of a C function to variable 
 before passing it into another function or?
No. You probably have stack corruption. Does it crash if FMOD_System_Create returns ok?
Sep 15
parent Timothy Foster <timfost aol.com> writes:
On Friday, 15 September 2017 at 16:55:27 UTC, Kagamin wrote:
 On Friday, 15 September 2017 at 04:01:13 UTC, Timothy Foster 
 wrote:
 am I required to save the result of a C function to variable 
 before passing it into another function or?
No. You probably have stack corruption. Does it crash if FMOD_System_Create returns ok?
FMOD_System_Create is always returning FMOD_OK. I have zero issues when I save the result of a fmod function to a variable before passing it to my helper function. Sometimes crashes: ErrorFMOD(Some_FMOD_Function(...), "print text"); Never crashes: auto result = Some_FMOD_Function(...); ErrorFMOD(result, "print text"); The crashes happen even if ErrorFMOD is an empty function: void ErrorFMOD(int r, string s = ""){ //nothing happening }
Sep 15
prev sibling parent Johan Engelen <j j.nl> writes:
On Friday, 15 September 2017 at 04:01:13 UTC, Timothy Foster 
wrote:
 I've been calling it like so:

 ErrorFMOD(FMOD_System_Create(&system), "Error Creating System: 
 ");

 Making the calls without my helper function doesn't cause an 
 Access Violation.
 Calling it like this is the only thing that seems to fix it:

 auto result = FMOD_System_Create(&system);
 ErrorFMOD(result, "Error Creating System: ");

 Is this a known issue, or am I required to save the result of a 
 C function to variable before passing it into another function 
 or?
This is very strange and you are certainly not required to save the result in a temp variable first. Do you have a small but full testcase that we can look at? (did you try with another compiler, LDC or GDC?) (note that debug information may be off, so the crash may happen in a different location from where it is reported to happen) -Johan
Sep 17