Thread overview
Access Violation when passing the result of a C function directly to a D function?
Sep 15, 2017
Timothy Foster
Sep 15, 2017
Vadim Lopatin
Sep 15, 2017
Timothy Foster
Sep 15, 2017
Kagamin
Sep 15, 2017
Timothy Foster
Sep 17, 2017
Johan Engelen
September 15, 2017
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?
September 15, 2017
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
September 15, 2017
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.
September 15, 2017
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?
September 15, 2017
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
}
September 17, 2017
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