Allocatememory in a dll

Just starting out? Need help? Post your questions and find answers here.
jassing
Addict
Addict
Posts: 1775
Joined: Wed Feb 17, 2010 12:00 am

Allocatememory in a dll

Post 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?
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Allocatememory in a dll

Post by Olliv »

DLL dynamic memory seems to be standalone.

Code: Select all

ProcedureDLL GetMemSize(*P)
   ProcedureReturn MemorySize(*P)
EndProcedure
User avatar
idle
Always Here
Always Here
Posts: 5098
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Allocatememory in a dll

Post 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
Windows 11, Manjaro, Raspberry Pi OS
Image
jassing
Addict
Addict
Posts: 1775
Joined: Wed Feb 17, 2010 12:00 am

Re: Allocatememory in a dll

Post 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...
User avatar
idle
Always Here
Always Here
Posts: 5098
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Allocatememory in a dll

Post 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.
Windows 11, Manjaro, Raspberry Pi OS
Image
jassing
Addict
Addict
Posts: 1775
Joined: Wed Feb 17, 2010 12:00 am

Re: Allocatememory in a dll

Post by jassing »

Awesome. thanks.
Post Reply