I ran into this problem, I allocate memory in a dll; but when I try to free it in my program, it fails.
Here's complete test code, save it as 'test.pb' -- then create executable, which will create the dll; then hit f5 to compile/run (as an exe) and when I try to examine or free the memory, it fails.
Code: Select all
; compile 1st to a dll "test.dll" (create executable)
; then compiler/run the file (just press f5)
;
CompilerIf #PB_Compiler_ExecutableFormat = #PB_Compiler_DLL
ProcedureDLL AllocateMemoryFromString( str.s )
Protected StringLength = StringByteLength( str )+SizeOf(Character)
*p = AllocateMemory( StringLength )
PokeS(*p, str)
ProcedureReturn *p
EndProcedure
ProcedureDLL FreeMemoryDLL( address ) ; As I read it; this is technically illegal to do for a released application (Can't just wrap PB functions)
ProcedureReturn FreeMemory(address)
EndProcedure
CompilerElse
Import "test.lib"
AllocateMemoryFromString( c.s )
FreeMemoryDLL( address )
EndImport
*p = AllocateMemoryFromString("This is a test for AllocateMemoryFromString() in a dll")
Debug Hex(*p) ;< WORKS
Debug PeekS(*p) ;< WORKS
Debug MemorySize(*P);< FAILS, comment it out and....
FreeMemory(*p) ;< FAILS too, comment it out and ...
Debug FreeMemoryDLL( *p ) ;< WORKS,
CompilerEndIf
; IDE Options = PureBasic 5.70 LTS beta 2 (Windows - x64)
; ExecutableFormat = Shared dll
; CursorPosition = 2
; Folding = -
; EnableThread
; EnableXP
; Executable = test.dll
; CompileSourceDirectory
Why does the the freememory fail in the exe, but not in the dll?