www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Class instance becoming null after calling bindings to C code???

reply Chirs Forest <CF chrisforest.com> writes:
I'm using the derelict fmod bindings to handle sounds in my 
application and I'm running into a weird bug... If I put calls to 
fmod in a function inside a class, upon calling those functions 
the instance of that class will be null, or otherwise changed. I 
obviously get an access violation if I try to do anything with 
that instance of the class while it's null.

class Audio {
     FMOD_SOUND* audio;
     FMOD_SYSTEM* fmod;

     this(){
         //initialization code
     }
     void load(string path){
         FMOD_System_CreateSound(fmod, path.toStringz, 0, null, 
&audio);
     }
}

Audio a;

void main(){
     writeln(&a); // 281478
     a = new Audio();
     writeln(&a); // 281478
     a.load("audio.ogg");
     writeln(&a); // null
     ... //some time later
     writeln(&a); // 281478
}

It happens even when I rearrange example code to be inside a class
https://github.com/Extrawurst/DerelictFmod/blob/v4.1.0/source/app.d

The calls work and don't return an error code. I can play music 
as long as I load and play within the same function call. Just 
the surrounding class reference gets messed up? And if I check 
later in the program the instance will return to its former value.

I'm using Windows 7 x64. Just updated to the latest DMD version. 
I'm compiling as an x86 Windows application. Got the most recent 
bindings and dlls.

I don't understand what's making this happen... any help would be 
very much appreciated.
Nov 15 2017
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 15 November 2017 at 13:24:02 UTC, Chirs Forest 
wrote:
 class Audio {
 Audio a;
     writeln(&a); // 281478
&class is usually wrong. That's the address of the reference, not of the actual object. You might want `cast(void*) a` instead to print the address of the object itself.
     writeln(&a); // null
Though why it would ever say null is weird. Maybe one of the lengths you pass to a C function is wrong and it is writing too far and killing one of the hidden thread local pointer variables.
Nov 15 2017
parent Chirs Forest <CF chrisforest.com> writes:
On Wednesday, 15 November 2017 at 13:30:14 UTC, Adam D. Ruppe 
wrote:
 On Wednesday, 15 November 2017 at 13:24:02 UTC, Chirs Forest 
 wrote:
 class Audio {
 Audio a;
     writeln(&a); // 281478
&class is usually wrong. That's the address of the reference, not of the actual object. You might want `cast(void*) a` instead to print the address of the object itself.
     writeln(&a); // null
Though why it would ever say null is weird. Maybe one of the lengths you pass to a C function is wrong and it is writing too far and killing one of the hidden thread local pointer variables.
The problem isn't that it's printing null, the problem is that I'm getting an access violation at the points where writeln will return null. writeln(cast(void*)a) gives me slightly different results: It'll start off as null, print an address after the class has been created, and then print a different address after calling load(). I'm not passing any lengths... I'm passing a string, but the function doesn't take a string length (I'm using toStrings to make the cstring and I've tried adding \0 at the end of it too, but it doesn't help). The function takes a struct for options but is meant to take null if you're not doing anything special. Replacing that with a pointer to an instance of that struct will give me an access violation calling writeln(cast(void*)a) after load() (that struct takes the size of itself as its first member and that's set properly). I'm not really sure what fmod is doing after I call it. Even calling the function to control volume messes with the class address... it might be messing with more than just that, so if it's possible for fmod to be overflowing into the context that called it, that's probably it. Is there anything I can do about it or should i be contacting the fmod guys, or the derelict binding guys?
Nov 15 2017
prev sibling next sibling parent Kagamin <spam here.lot> writes:
See 
https://forum.dlang.org/post/nwzybduqoljluinnaqqp forum.dlang.org
Nov 17 2017
prev sibling parent Kagamin <spam here.lot> writes:
And https://github.com/Extrawurst/DerelictFmod/issues/1
Nov 17 2017