CreateFileW and memory leak

BlitzMax Forums/BlitzMax Programming/CreateFileW and memory leak

Volker(Posted 2010) [#1]
Hi,

I'm using the windows function CreateFileW to get the
filesize of files bigger then 2 Gb.
But there seems to be a memory leak, or is it in my code?
44 bytes are lost every call.
Repeat
Notify("Ready?")
For Local i:Int = 1 To 3000
	FileSizeEx("d:\temp\1.txt") ' change this to an existing file
	Delay 1
Next
GCCollect()
Notify("Finished")
Forever


Extern "win32"
	Function CreateFileW(lpFileName:Byte Ptr, dwDesiredAccess:int, dwShareMode:Int, lpSecurityAttributes:Byte Ptr, dwCreationDisposition:int, dwFlagsAndAttributes:int, hTemplateFile:int)
	Function GetFileSizeEx(hFile:int, lpFileSize:Byte Ptr)
	Function CloseHandle(hObject:Int)
EndExtern


Function FileSizeEx:Long(path:String) ' memory leak in createfileW()?
	Global size:Long, handle:Int

	handle=CreateFileW(path.ToWString(),GENERIC_READ,0,Null,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0)
	If handle<>INVALID_HANDLE_VALUE
		If GetFileSizeEx(handle, Varptr size) = 0 Then size = -1
		CloseHandle(handle)
	EndIf
	handle = Null
	Return size
EndFunction




Zeke(Posted 2010) [#2]
local WPath:short ptr=path.ToWString()
handle=CreateFileW(WPath, .....)
...
handle=Null
MemFree WPath


or

Extern "win32"
Function CreateFileW(lpFileName$z, dwDesiredAccess:Int, dwShareMode:Int, lpSecurityAttributes:Byte Ptr, dwCreationDisposition:Int, dwFlagsAndAttributes:Int, hTemplateFile:Int)
...
handle=CreateFileW(Path, .....)



Volker(Posted 2010) [#3]
Nice, you found the guilty one.
Thanks a lot.


Zeke(Posted 2010) [#4]
hmm. i just found that second example is wrong. lpFileName$z is same as string.ToCString() (without memfree), BUT you can also use lpFilename$w which is same as string.ToWString().

so i think this is correct way to do this:
Extern "win32"
Function CreateFileW(lpFileName$w, dwDesiredAccess:Int, dwShareMode:Int, lpSecurityAttributes:Byte Ptr, dwCreationDisposition:Int, dwFlagsAndAttributes:Int, hTemplateFile:Int)