DLL returned string memory leak

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
visualfreebasic5
Posts: 25
Joined: Dec 11, 2019 15:14
Contact:

DLL returned string memory leak

Post by visualfreebasic5 »

Write a function that returns string in DLL
If this function is called in exe, a memory leak will occur
Example:
DLL

Code: Select all

Function Myaaa1() As String Export '
   Function = "String"
End Function
EXE

Code: Select all

Declare Function Myaaa1 Lib "FunSupLib" Alias "MYAAA1"() As String 
   Dim i As Long  ,ss As String  
   For i=0 To 100000
      ss = Myaaa1()
   Next 
After running exe, EXE will occupy more and more memory
I don't know English. I translated it with Google. I don't know whether the expression is understood by everyone.
In this case, what can be done to solve it?
Is this a bug?
marcov
Posts: 3455
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: DLL returned string memory leak

Post by marcov »

Make sure that mainprogram and dll use the same memory manager. Are both sides in FB ?
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: DLL returned string memory leak

Post by dodicat »

You can use a pointer.

Code: Select all


Function Myaaa1() As string ptr Export 
    static as string s:s="String"
   Function = @s
End Function 
...

Code: Select all


Declare Function Myaaa1 Lib "FunSupLib" Alias "MYAAA1"() As String ptr
   Dim i As Long  ,ss As String  
   For i=0 To 100000
      ss = *Myaaa1()
   Next
   print
   print "ss = ";ss;" ";

   sleep 
a string ptr or zstring ptr using function=strptr(s), and the return value of Myaaa1 as zstring ptr
But zstring will stop at any chr(0), but it would do fine in your example, probably better than string ptr
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: DLL returned string memory leak

Post by fxm »

@Jeff,
It (above) looks like a bug when the DLL function returns by value a string.
The temporary string does not seem to be properly destroyed.

For now, let's use a return by pointer as suggested by dodicat above, or alternatively a return by reference:

Code: Select all

Function Myaaa1() Byref As String Export
    Static As String s
    s = "String"
    Function = s
End Function
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: DLL returned string memory leak

Post by caseih »

What does "return by value" for a string mean? That the string returned is a temporary copy of the original string?
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: DLL returned string memory leak

Post by fxm »

Yes.
visualfreebasic5
Posts: 25
Joined: Dec 11, 2019 15:14
Contact:

Re: DLL returned string memory leak

Post by visualfreebasic5 »

marcov wrote: Jun 08, 2022 13:16 Make sure that mainprogram and dll use the same memory manager. Are both sides in FB ?
Yes, it's all in FB
visualfreebasic5
Posts: 25
Joined: Dec 11, 2019 15:14
Contact:

Re: DLL returned string memory leak

Post by visualfreebasic5 »

fxm wrote: Jun 08, 2022 14:00 @Jeff,
It (above) looks like a bug when the DLL function returns by value a string.
The temporary string does not seem to be properly destroyed.

For now, let's use a return by pointer as suggested by dodicat above, or alternatively a return by reference:

Code: Select all

Function Myaaa1() Byref As String Export
    Static As String s
    s = "String"
    Function = s
End Function
Good method, thank you.
If multiple threads use functions at the same time, I don't know if there will be any problems.
visualfreebasic5
Posts: 25
Joined: Dec 11, 2019 15:14
Contact:

Re: DLL returned string memory leak

Post by visualfreebasic5 »

dodicat wrote: Jun 08, 2022 13:19 You can use a pointer.

Code: Select all


Function Myaaa1() As string ptr Export 
    static as string s:s="String"
   Function = @s
End Function 
...

Code: Select all


Declare Function Myaaa1 Lib "FunSupLib" Alias "MYAAA1"() As String ptr
   Dim i As Long  ,ss As String  
   For i=0 To 100000
      ss = *Myaaa1()
   Next
   print
   print "ss = ";ss;" ";

   sleep 
a string ptr or zstring ptr using function=strptr(s), and the return value of Myaaa1 as zstring ptr
But zstring will stop at any chr(0), but it would do fine in your example, probably better than string ptr
Thank you. This is a good way
visualfreebasic5
Posts: 25
Joined: Dec 11, 2019 15:14
Contact:

Re: DLL returned string memory leak

Post by visualfreebasic5 »

fxm wrote: Jun 08, 2022 14:00 @Jeff,
It (above) looks like a bug when the DLL function returns by value a string.
The temporary string does not seem to be properly destroyed.

For now, let's use a return by pointer as suggested by dodicat above, or alternatively a return by reference:

Code: Select all

Function Myaaa1() Byref As String Export
    Static As String s
    s = "String"
    Function = s
End Function
After testing, this code also has a memory leak.
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: DLL returned string memory leak

Post by fxm »

For me, This works !
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: DLL returned string memory leak

Post by fxm »

A bug report is already open since 2020:
#620 DLL, function as string called multiple times, faint
I see there is no easy fix !

See also the forum topic:
problem export functiion () as string
visualfreebasic5
Posts: 25
Joined: Dec 11, 2019 15:14
Contact:

Re: DLL returned string memory leak

Post by visualfreebasic5 »

I see. The following method is the best solution.
Thank you very much for helping me solve the problem.

Code: Select all

Function getmidiparamtest () ByRef  As String  Export
It is the first time that I have seen such a way of writing."ByRef" Learned new knowledge.
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: DLL returned string memory leak

Post by fxm »

Another possible workaround is to use a 'sub foo( byref result as string )' procedure parameter to return a string result.
Post Reply