xLeaves
25-05-2022, 07:57
I often implement DLLs to extend the functionality of thinBasic. These DLLs were originally written using FreeBasic, and I later started using TCC to implement my DLLs.
I find that when exchanging data between thinBasic and DLLs implemented in other programming languages, strings are always difficult to use as return values, unless my return value type is BSTR.
So I often needed wrappers such as
Declare Function Process_ListChild_Ori Lib "xLib.dll" Alias "Process_ListChild" (ByVal pHdr As Long) As ASCIIZ Ptr
Function Process_ListChild(ByVal pHandle As Long) As String
Dim sPtr As Long = Process_ListChild_Ori(pHandle)
Dim sRet As String = Peek$(ASCIIZ, sPtr)
Return sRet
End Function
Then I got used to using BSTR, but found that my scripts often crashed and were very random. After a few days of troubleshooting, I came across this passage in a search engine
BSTR is always free by the caller, the called party does not need to free the BSTR.
It seems that ThinBASIC is the same way, so I removed the code for GC to reclaim BSTR memory, and now they work fine and run for a long time without crashing, sharing it to avoid people who have the same problem as me to suffer difficulties.
Translated with www.DeepL.com/Translator (free version)
I find that when exchanging data between thinBasic and DLLs implemented in other programming languages, strings are always difficult to use as return values, unless my return value type is BSTR.
So I often needed wrappers such as
Declare Function Process_ListChild_Ori Lib "xLib.dll" Alias "Process_ListChild" (ByVal pHdr As Long) As ASCIIZ Ptr
Function Process_ListChild(ByVal pHandle As Long) As String
Dim sPtr As Long = Process_ListChild_Ori(pHandle)
Dim sRet As String = Peek$(ASCIIZ, sPtr)
Return sRet
End Function
Then I got used to using BSTR, but found that my scripts often crashed and were very random. After a few days of troubleshooting, I came across this passage in a search engine
BSTR is always free by the caller, the called party does not need to free the BSTR.
It seems that ThinBASIC is the same way, so I removed the code for GC to reclaim BSTR memory, and now they work fine and run for a long time without crashing, sharing it to avoid people who have the same problem as me to suffer difficulties.
Translated with www.DeepL.com/Translator (free version)