Page 1 of 1

Allocatememory in a dll

Posted: Mon Nov 12, 2018 4:32 am
by jassing
I only tested this on windows, not sure if it's a windows only issue.

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
But if I use FreeMemoryDLL() that just wraps FreeMemory() it works.
Why does the the freememory fail in the exe, but not in the dll?

Re: Allocatememory in a dll

Posted: Mon Nov 12, 2018 5:58 am
by Olliv
DLL dynamic memory seems to be standalone.

Code: Select all

ProcedureDLL GetMemSize(*P)
   ProcedureReturn MemorySize(*P)
EndProcedure

Re: Allocatememory in a dll

Posted: Mon Nov 12, 2018 6:17 am
by idle
the dll is using it's own heap, so you have to free the memory it in the module you allocate it in
you can get around it using the heapAlloc HeapFree HeapSize and pass GetProcessHeap into you dll attach process
or per function

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(heap,str.s )
    Protected StringLength = StringByteLength( str )+SizeOf(Character)
    *p = HeapAlloc_(heap,8,StringLength)
    CopyMemory(@str,*p,StringLength)
    
    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 "testdll.lib"
    AllocateMemoryFromString(heap.i,c.s )
    FreeMemoryDLL( address )
  EndImport
  
  heap = GetProcessHeap_() 
  
  *p = AllocateMemoryFromString(heap,"This is a test for AllocateMemoryFromString() in a dll")
  
  Debug Hex(*p)       ;< WORKS
  Debug PeekS(*p)     ;< WORKS
  Debug HeapSize_(heap,0,*P);< FAILS, comment it out and....
  Debug HeapFree_(heap,0,*p)      ;< FAILS too, comment it out and ...
   
  
CompilerEndIf

Re: Allocatememory in a dll

Posted: Mon Nov 12, 2018 6:32 am
by jassing
Cool; thank you for the explanation (& code)
But isn't that against the EULA? I seem to recall reading something about not being able to distribute a dll with functions that just wrap a PB function...

Re: Allocatememory in a dll

Posted: Mon Nov 12, 2018 6:54 am
by idle
jassing wrote:Cool; thank you for the explanation (& code)
But isn't that against the EULA? I seem to recall reading something about not being able to distribute a dll with functions that just wrap a PB function...
That's more to do with intent, like wrapping a whole pb library and flogging it off as your own.
So there's no issue with wrapping Freememory in a function, it's kind of necessary.

Re: Allocatememory in a dll

Posted: Mon Nov 12, 2018 7:05 am
by jassing
Awesome. thanks.