Strange memory corruption / codegen bug?

TheGag96 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Dec 11 03:15:24 PST 2016


I was porting my Evolutionary Computing homework written in 
Python over to D, and I've come across this bug I cannot for the 
life of me figure out.

https://gitlab.com/TheGag96/evo-pacman/blob/master/source/pacman/tree.d#L139

I don't think I could cut this down to a smaller reproducible 
scenario due to how bizarre and specific this problem is, so I 
apologize in advance if this is hard to follow.

Basically, imagine that I have a binary tree class where, due to 
how my program is set up, each node will have either children on 
both the left and right side or no children at all (the latter 
represented by a value of null for Tree's left and right member). 
I have a member function called "dup" -- marked const, mind you 
-- that just returns a deep copy of the tree and SHOULDN'T make 
any changes to the calling object.

I call this function a couple different places and it appears to 
function okay, but at the spot I linked, if I call .dup here, it 
will corrupt one of the tree's nodes and put something in its 
left member for no apparent reason. This oddly doesn't happen on 
every Tree calling this function. This could definitely be some 
stupid mistake on my part, but the fact that calling a 
const-marked function changes the state of the calling object 
makes me think something else is afoot here...

I would greatly appreciate anyone who would be willing to take a 
look at this. This bug is driving me absolutely nuts.


More information about the Digitalmars-d-learn mailing list