It is currently Sun Dec 16, 2018 6:52 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Allocatememory in a dll
PostPosted: Mon Nov 12, 2018 4:32 am 
Offline
Addict
Addict
User avatar

Joined: Wed Feb 17, 2010 12:00 am
Posts: 1324
Location: (Embarrassed to say country)
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:
; 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?


Top
 Profile  
Reply with quote  
 Post subject: Re: Allocatememory in a dll
PostPosted: Mon Nov 12, 2018 5:58 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Tue Sep 22, 2009 10:41 pm
Posts: 325
DLL dynamic memory seems to be standalone.
Code:
ProcedureDLL GetMemSize(*P)
   ProcedureReturn MemorySize(*P)
EndProcedure


Top
 Profile  
Reply with quote  
 Post subject: Re: Allocatememory in a dll
PostPosted: Mon Nov 12, 2018 6:17 am 
Offline
Addict
Addict
User avatar

Joined: Fri Sep 21, 2007 5:52 am
Posts: 3346
Location: New Zealand
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:
; 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

_________________
Got winter blues?
Enjoy a Caravan Trip into, "The Land of Grey and Pink", wine and punk weed optional!
https://www.youtube.com/watch?v=9hmFzGTxod4


Top
 Profile  
Reply with quote  
 Post subject: Re: Allocatememory in a dll
PostPosted: Mon Nov 12, 2018 6:32 am 
Offline
Addict
Addict
User avatar

Joined: Wed Feb 17, 2010 12:00 am
Posts: 1324
Location: (Embarrassed to say country)
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...


Top
 Profile  
Reply with quote  
 Post subject: Re: Allocatememory in a dll
PostPosted: Mon Nov 12, 2018 6:54 am 
Offline
Addict
Addict
User avatar

Joined: Fri Sep 21, 2007 5:52 am
Posts: 3346
Location: New Zealand
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.

_________________
Got winter blues?
Enjoy a Caravan Trip into, "The Land of Grey and Pink", wine and punk weed optional!
https://www.youtube.com/watch?v=9hmFzGTxod4


Top
 Profile  
Reply with quote  
 Post subject: Re: Allocatememory in a dll
PostPosted: Mon Nov 12, 2018 7:05 am 
Offline
Addict
Addict
User avatar

Joined: Wed Feb 17, 2010 12:00 am
Posts: 1324
Location: (Embarrassed to say country)
Awesome. thanks.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 14 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye