WTF is going on! Corrupt value that is never assigned

FoxyBrown via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 13 15:53:45 PDT 2017


On Thursday, 13 July 2017 at 20:35:19 UTC, Moritz Maxeiner wrote:
> On Thursday, 13 July 2017 at 18:22:34 UTC, FoxyBrown wrote:
>> The following code is pretty screwed up, even though it 
>> doesn't look like it. I have a buf, a simple malloc which hold 
>> the results of a win32 call. I am then trying to copy over the 
>> data in buf to a D struct.
>>
>> But when copying the strings, the buf location changes, 
>> screwing up the copying process. It shouldn't happen, buf 
>> never changes value anywhere except the first malloc(which is 
>> once). Somehow it is getting changed, but where?
>>
>> [...]
>>
>> The buf value changes when calling cstr2dstr but I've had it 
>> with other values to(any function call such as to!string, etc 
>> seems to trigger it).
>>
>> [...]
>
> - Does this happen every time, or only sometimes?
yes, but I've been having this problem and not sure if it was 
quite as consistent as before or that I just recognized it.

> - At which loop iteration does it occur?
Now it seems to occur after the first iteration, but I've add it 
happen after a while and in other cases it's worked.. depends on 
if I use malloc, or a D array, or what.

> - Which compiler (+version) are you using (with what flags)?
Latest DMD official.. whatever default flags exist in debug mode 
with visual D... why should it matter? buf is changing on the 
only source of that change could be through the winapi call or 
the temp pointer used to index it.. which is never assigned to, 
so it can't be modifying it. The c2d function, when called, 
clearly has no understanding of buff, yet after it returns, it is 
causing the problem. This seems like the stack is being corrupted 
by the function call.


> - What are the steps to reproduce (i.e. does this e.g. happen 
> with a main that consist of one call to EnumServices) ?


Yes, It is basically the first thing I do when I run my program. 
It is a rather isolated function(Just trying to get a list of 
current services, which has been a total PITA because D is not 
interfacing well with C. First, the win32 function  does not 
simply fill in an array but adds additional junk at the 
end(didn't know that until after a few wasted hours trying to get 
it to fill in an array properly). Hence now I'm trying to convert 
the returned data one iteration at a time rather than all at 
once, but I can't get that to work because the pointer to the 
buffer I created is changing. I could use a temp and get it to 
work, but that doesn't explain what the hell is going on. The 
value of buff seems to be erratic, I've had it point valid 
stuff(other data) and then have small values in it like 8.

I don't know how any stack corruption could be occurring but that 
is exactly what it looks like. "Return from function call and 
"static variables"(with respect to the call) are changed.". But 
that seems really hard to sell given that it's pretty simple and 
D should have all those basics well covered.



More information about the Digitalmars-d-learn mailing list