Access Violation when passing the result of a C function directly to a D function?

Vadim Lopatin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Sep 15 10:33:55 UTC 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


More information about the Digitalmars-d-learn mailing list