WTF is going on! Corrupt value that is never assigned

FoxyBrown via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 13 17:33:12 PDT 2017


On Thursday, 13 July 2017 at 23:30:39 UTC, Moritz Maxeiner wrote:
> On Thursday, 13 July 2017 at 22:53:45 UTC, FoxyBrown wrote:
>> 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? [...]
>
> Because it's part of the usual "Steps to reproduce" you are 
> supposed to provide so others can verify what you're 
> encountering.
>
>>> - 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. [...]
>
> Okay, I'll setup a Windows VM when I have time and check it out 
> (unless someone solves it beforehand).
>
>> 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).
>
> To be fair, that's neither C nor D fault; that's Microsoft 
> providing unintuitive, horrible APIs and doing an amazing job 
> of providing documentation (MSDN) that *appears* to be 
> exhaustive and well written, but misses all these little 
> important details that you actually have to know in order to 
> program correct control logic, driving you to the edge of 
> sanity. Been there, done that.

I have generally not had that problem. Usually works as it does 
and pretty straight forward. Might have to fish a little for info 
from others but most things have been worked out by someone 
somewhere.

What I'm trying to do is fairly straightforward but I've wasted 
nearly 2 days on it. I had no issues starting or stopping 
services but I can't get the enumerate code to work(the winapi 
function works fine, it's getting the info out of what it passes 
in to D that is the problem). There is no explanation why buf is 
being changed, I never change it after it is initialized, yet, it 
changes somehow. Either it is being overwritten because of a 
pointer that is invalid or it is a stack problem. It is most 
likely the former, but I have tried many different ways and they 
all lead to similar results.




More information about the Digitalmars-d-learn mailing list