I have a really weird bug in my application, i succeeded in making a small program with the bare munimum to show that bug.

The full source code of the application + a dub.json file are at https://github.com/Begah/D_Pointer_Problem ( < 300 LOC )

I have a template called ResourceManager (asset.d) which takes in an asset ( Texture or Model ) and return a handle to it. A handle has a pointer to the asset as well as a pointer to an integer ( referenceCounter ) both allocated by C's malloc.

First, i call ResourceManager!(Model).get("Coin.obj"). This function checks if that resource is already loaded, if not ( and in this case it's not ) then it calls model.loadModel(); A model has an array of textures, for simplicity sakes, in this example i only add one texture. Before model.loadModel returns the struct created, i first prints the location and value of the referenceCounter of the first texture in the model :
        Logger.info(model.textures[0].referenceCount);
        Logger.info(*model.textures[0].referenceCount);
        
For example it prints :
INFO (source\model.d|38) : 597F08 <- Location of the referenceCounter INFO (source\model.d|39) : 2 <- Value of the referenceCounter
        
Next, model.loadModel return the model structure to ResourceManager!(Model).get("Coin.obj") :
        AssetType asset = loadFunc(asset_name, args);

        static if(is(model.Model == AssetType)) {
                Logger.info(asset.textures[0].referenceCount);
                Logger.info(*asset.textures[0].referenceCount);
                Logger.info(asset.textures[0].referenceCount);
        }
        
And this is where my application, when ResourceManager!(Model).get("Coin.obj") prints the location and value of the first texture's referenceCounter.
This prints :
INFO (source\assets.d|83) : 597F08 <- Location of the referenceCounter INFO (source\assets.d|84) : 1 <- Value of the referenceCounter INFO (source\assets.d|85) : 434A7C <- Location of the referenceCounter ( again )
        
These three lines of code does nothing except prints to the console. As you can see, for some reason, the location of the referenceCounter changes for no apparant reason. I have done many different test and changes but i can't understand why this bug is happening, any ideas?

Reply via email to