CopyStructure Problem with DLL in 5.71 beta 1 LTS

Just starting out? Need help? Post your questions and find answers here.
Cyllceaux
Enthusiast
Enthusiast
Posts: 469
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

CopyStructure Problem with DLL in 5.71 beta 1 LTS

Post by Cyllceaux »

This works how expected.

Code: Select all

EnableExplicit

Structure str2
	tada.s
EndStructure

Structure str1
	name.s
	wert.i
	List liste.str2()
EndStructure

ProcedureDLL initS()
	ProcedureReturn AllocateStructure(str1)
EndProcedure


Define *original.str1=initS()
Define *copy.str1=AllocateStructure(str1)

CopyStructure(*original,*copy,str1)
CopyStructure(*copy,*original,str1)

Debug "OK"
Now I create a DLL (test.dll) with the code above and run the code below.

Code: Select all

EnableExplicit

Structure str2
	tada.s
EndStructure

Structure str1
	name.s
	wert.i
	List liste.str2()
EndStructure

Prototype INITTEST()

If OpenLibrary(0,"test.dll")
	Define initS.INITTEST=GetFunction(0,"initS")
Else
	Debug "Fehler"
EndIf


Define *original.str1=initS()
Define *copy.str1=AllocateStructure(str1)

CopyStructure(*original,*copy,str1)
CopyStructure(*copy,*original,str1) ; Application dies

Debug "OK"
The Application dies on the second CopyStructure. The application closed without any message
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: CopyStructure Problem with DLL in 5.71 beta 1 LTS

Post by Demivec »

A quote from this thread A helping hand needed with dll's:
As long as the DLL does not attempt to free the memory allocated by the client (or vise-versa) then you should be okay.
Your code attempts to free memory allocated in the DLL when it overwrites the structure during execution of

Code: Select all

CopyStructure(*copy,*original,str1) ; Application dies
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: CopyStructure Problem with DLL in 5.71 beta 1 LTS

Post by Mijikai »

To make this work u should either handle this within the Program or the DLL.

Sample DLL:

Code: Select all

EnableExplicit

Structure STR2_STRUCT
   tada.s
EndStructure

Structure STR1_STRUCT
   name.s
   wert.i
   List liste.STR2_STRUCT()
EndStructure

ProcedureDLL.i Init()
  ProcedureReturn AllocateStructure(STR1_STRUCT)
EndProcedure

ProcedureDLL.i Copy(*Dst,*Src)
  CopyStructure(*Src,*Dst,STR1_STRUCT)
  ProcedureReturn #Null
EndProcedure
Sample Program:

Code: Select all

EnableExplicit

Import "dummy.lib"
  Init.i()
  Copy.i(*Dst,*Src)
EndImport

Structure STR2_STRUCT
  tada.s
EndStructure

Structure STR1_STRUCT
  name.s
  wert.i
  List liste.STR2_STRUCT()
EndStructure

Global *test1.STR1_STRUCT
Global *test2.STR1_STRUCT

*test1 = Init()
*test2 = Init()

Copy(*test2,*test1)
Copy(*test1,*test2)

Debug "ok"
Cyllceaux
Enthusiast
Enthusiast
Posts: 469
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: CopyStructure Problem with DLL in 5.71 beta 1 LTS

Post by Cyllceaux »

My problem is, I have to "transport" the structures between DLLs an application.

I have my working*copys of structures and the original ones.

Maybe I make json from the structures and send only strings between the DLLs
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: CopyStructure Problem with DLL in 5.71 beta 1 LTS

Post by Mijikai »

Cyllceaux wrote:My problem is, I have to "transport" the structures between DLLs an application.

I have my working*copys of structures and the original ones.

Maybe I make json from the structures and send only strings between the DLLs
Converting stuff to json and back is really slow.
Define the functions for structure allocation and copy in the main program and pass them to the dlls so every dll uses the same memory routines.
If u have many memory functions it might be handy to create an interface for that.
Cyllceaux
Enthusiast
Enthusiast
Posts: 469
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: CopyStructure Problem with DLL in 5.71 beta 1 LTS

Post by Cyllceaux »

OK... This is what I do.

I have a big program. (more than 300k lines of code) Everything in one execution file. I make it smaller with export lots of function to DLLs.

I have a DLL for my database. My Customers have some kind of JSON Database.
So I load and save everything in json. The program works on medical datas… A LOT OF Medical datas in different parts of the json structures.

For example, I have some DLL to "clean" chromatography-datas, some of enrich the the datas with massspectrometry- datas (maldi) and parts of personal datas. The json files are compressed and secured. I only work with parts of the datas… not the complete bunch. If I finish my work, I update the single parts of the datatree (the reason of *original and *copy) and save them again.

The next I want to extract these to DLLs, I have some branches of the software. (I use fossil) with some new UI and so on... And I don't want to copy alle these code everytime in every Branche, when I can use a dll.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: CopyStructure Problem with DLL in 5.71 beta 1 LTS

Post by Mijikai »

My english is not very good so i dont really get what you want to do :oops:

But i can think of another possible solution involving some black magic (hack).
-> You could probably also patch the memory heap of the dll so all code works on the same memory heap.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: CopyStructure Problem with DLL in 5.71 beta 1 LTS

Post by Mijikai »

Mijikai wrote:
-> You could probably also patch the memory heap of the dll so all code works on the same memory heap.
I just tested this and it did not work!
The copy functions still mess up the heap :?

Anyway heres some black magic for the heap if anyone wants to dive in further 8)
(Debugger needs to be disabled since it changes the opcodes!)

Code:

Code: Select all

;Author Mijikai
;PatchHeap() if Heap.i is #Null it returns the heap otherwise it will change the heap!
Procedure.i PatchHeap(Heap.i = #Null);GET & SET (Structure) HEAP (Tested with PureBasic 5.62 / Windows x64 only!)
  Protected *offset.Long
  Protected *marker.Ascii
  Protected *heap.Integer
  *offset = ?patch_heap + $D
  *marker = *offset\l + *offset + $4
  Repeat
    If *marker\a = $E8
      *offset = *marker + $1 
      *marker = *offset\l + *offset + $4
      *offset = *marker + $B
      *heap = *offset\l + *offset + $4
      If Heap
        *heap\i = Heap
        ProcedureReturn #True
      Else
        ProcedureReturn *heap\i
      EndIf
    EndIf
    *marker + $1
  Until *marker\a = $C3
  ProcedureReturn #Null
  patch_heap:
  AllocateStructure(Byte)
EndProcedure
Fred
Administrator
Administrator
Posts: 16687
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: CopyStructure Problem with DLL in 5.71 beta 1 LTS

Post by Fred »

That's right, CopyStructure() internally free previous structure elements, so it can't work that way (needs to be in the DLL as well, as it has its own memory space).
Cyllceaux
Enthusiast
Enthusiast
Posts: 469
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: CopyStructure Problem with DLL in 5.71 beta 1 LTS

Post by Cyllceaux »

Thanks @all... and good to know...

So... "Plan B" :D :wink:
Post Reply