i got a problem, with some memory allocation stuff, and variable manipulation
the problem is when i do certain things, it clears (or changes) the variables,
for example when i use the CreateFile api, i got the filename in a global variable, which is fine
until after i call the api, it creates the file alright, but it clears out the filename variable (or changes the address, not sure)
it also does it with other stuff too, like when using the stack with certain variables.
i dunno what i'm doin wrong, do i have to know anything about addresses changing when calling certain functions ?
i'm using HeapAlloc with a local variable (everytime data arrives on the socket), i don't know whether that would affect anything, cause of it changing all the time ?
the main problem though is that the file handle clears, and so everytime i recieve data it clears the file handle, anyone know what i'm doin wrong ?
i could post some code if it'd help understand ?
Posted on 2004-02-17 01:15:47 by someone
when a function exits, all variables(local->Volatile) allocated at the stack dissipates. In your case, it relates to the stack.

Make it global.

p.s. I might have mix and matched some terms here, so bare with me. :grin:
Posted on 2004-02-17 03:46:29 by arkane
Hi someone,

What you are doing sounds very problematic. From your post I gather you are using HeapAlloc to allocate a buffer then using a local variable to hold the pointer. The problem with this approach is that you must free the heap before you leave the procedure. Failure to do so will result in the loss of the pointer, local varaibles are stored on the stack and that stack is demolished (sort of) when you leave the proc. When that happens you no longer have a pointer because the variable no longer exists and you cannot reliably deallocate the heap. If you are allocating it each time you enter the procedure you are just biting off a new chunk of memory with each call and never freeing it. You will run out of memory eventually and be forced to shut down your application.

The rule is that anything that is defined as LOCAL does not exist once you ret from the procedure. Anything that is dependant on that variable (such as heaps, bitmaps etc..) should be destroyed before you return as well other wise you will not be able to retrieve the information necessary to do it later (bitmaps selected into a DC being the exception here). If you wish to use the buffer outside of the procedure assign it a global handle.
Posted on 2004-02-17 03:56:53 by donkey
i am using local variables as the heap pointers, but i do free the heap each time at the end of the proc, unless it's more than the one call;
invoke HeapFree, hHeap, HEAP_NO_SERIALIZE, 0

the problem isn't with the local variables getting cleared, they are all fine, but it's with the global variables, i have the filename, the file handle, and 1 other variable as global, and everytime i call certain functions, it clears the variable, not at the end of the proc, but directly after calling the api
Posted on 2004-02-17 04:27:04 by someone
could you specify/post the offending code(especially the function/s your talking about)?
Posted on 2004-02-17 04:30:35 by arkane
Hi someone, you will have to post an example of what you mean becasue it is not clear. The API does not overwrite memory unless it explicitly says it will (which CreateFileA does not). So the most likely scenario is that you are calling a function that writes to memory and the buffer you have assigned to it is not big enough and it is overflowing into other parts of your program. For example:

buffer db 256 DUP (0)
handle DD 0
cbRead DD 0

invoke ReadFile,,offset buffer,MAX_PATH,offset cbRead,NULL

Will overwrite the handle as MAX_PATH is 260 bytes and it will continue to write past the end of buffer.
Posted on 2004-02-17 04:33:16 by donkey
try checking your code flow(trace the code from the order of execution) and check each functions description/actions, you might have missed something there.

Try rechecking your use of variables too, you might have mistyped it and used the other one.



it's easy to interchanged both.

IIRC, there is a function that is commonly used together with CreateFile that modifies memory - GetOpenFileName ... press cancel... pointed buffer becomes null. buffer[0] = 0
Posted on 2004-02-17 04:41:39 by arkane
ok, i went through it again, and got it working sort of,
there was some problem with copying from memory into the filename buffer, i'm using szMid function from the masm32 macros, is there a better way to copy from a memory allocation into a null-terminated string ?
also there was another problem, i was calling: mov szFileName, 0 to clear the buffer on a new connection, whats the best way to clear a byte buffer ?, and i have the same problem with dword, whats the best way to set the value of a dword as 0 setting it's value to 0, not the address, (probably stupid questions, but i'm confused)
Posted on 2004-02-17 05:09:57 by someone
Hi someone,

There are a few functions in the masm32 library and also lstrcpy. If you want to empty a string you can normally just mov zero into the first byte, mov String,BYTE PTR 0 in MASM. If you want to zero the whole buffer I use this:

ZeroMem FRAME pDest,nBytes

mov edx,[nBytes]
mov ecx,edx
and edx,3

shr ecx,2
xor eax,eax
mov edi,[pDest]
rep stosd

mov ecx,edx
rep stosb

Posted on 2004-02-17 05:12:30 by donkey
thanks donkey, and arkane
i got it workin now, i never knew of the ptr thing, but it works now, thanks
Posted on 2004-02-17 05:32:36 by someone