• Print

Author Topic: So where are these leaking string libraries and how do we stop them?  (Read 1203 times)

Clippy

  • Hero Member
  • *****
  • Posts: 19011
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
So where are these leaking string libraries and how do we stop them?
« on: January 12, 2015, 11:33:52 am »
Steve noted at N54 that some C DLL libraries leak or lose string memory when used as libraries in QB64.

So how is adding them to the language eliminating this problem? How could our libraries do so? Another _FREEkeyword?
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 SDL: QB64v0954-win15.7z

SMcNeill

  • Moderator
  • Hero Member
  • *****
  • Posts: 5699
Re: So where are these leaking string libraries and how do we stop them?
« Reply #1 on: January 12, 2015, 11:52:13 am »
http://www.qb64.net/forum/index.php?topic=12080.0

Above is one instance where strings are unsuitable for DECLARE LIBRARY, until a patch is put in place somewhere. 

Luke's second post describes how the memory leaks perfectly for us.
http://bit.ly/TextImage -- Library of QB64 code to manipulate text and images, as a BM library.

http://bit.ly/Color32 -- A set of color CONST for use in 32 bit mode, as a BI library.

Clippy

  • Hero Member
  • *****
  • Posts: 19011
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
Re: So where are these leaking string libraries and how do we stop them?
« Reply #2 on: January 12, 2015, 01:49:22 pm »
So how does making C functions into QB64 functions fix the situation?
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 SDL: QB64v0954-win15.7z

Robert Claypool

  • Sr. Member
  • ****
  • Posts: 395
Re: So where are these leaking string libraries and how do we stop them?
« Reply #3 on: July 17, 2015, 07:05:56 pm »
Nearly an educated guess:
Would treating C return strings similar to QB's STRING * size do anything?

Johny B.

  • Hero Member
  • *****
  • Posts: 1317
Re: So where are these leaking string libraries and how do we stop them?
« Reply #4 on: July 17, 2015, 09:51:02 pm »
Robert's on the right track: when we use qbs strings (the name for the internal STRING type), we can manage everything properly. Of course, you can't go modifying a random dll file to return qbs strings.

The class of libraries that have this problem is actually pretty small, and it *is* possible to work around it with some global variables and extra function calls (but that gets especially messy if you didn't write the original code to begin with).

Unless you're doing complex stuff with DECLARE LIBRARY, you never need to worry about this.
QB64 Daily Builds: Windows Linux Mac

Matir

  • Full Member
  • ***
  • Posts: 173
  • x^y=y^x,x+y=x-y^x What are the solutions??
Re: So where are these leaking string libraries and how do we stop them?
« Reply #5 on: July 18, 2015, 02:59:45 am »
I am curious to know the difference a qbs* and a char*... I thought qbs was just a typedef of char.
:):( ;)

SkyCharger001

  • Hero Member
  • *****
  • Posts: 2228
Re: So where are these leaking string libraries and how do we stop them?
« Reply #6 on: July 18, 2015, 04:15:41 am »
Just an idea: perhaps we could a Declare-Library-only _STRINGFUNCTION that tells QB64 to perform a string-conversion on what it returns.

Johny B.

  • Hero Member
  • *****
  • Posts: 1317
Re: So where are these leaking string libraries and how do we stop them?
« Reply #7 on: July 18, 2015, 04:24:27 am »
Matir: qbs is a structure that contains, amongst other things: the length of the string, information about whether it is readonly, and a pointer to the string data itself. This abstraction means you don't have to worry about CHR$(0)'s getting in the way, since they would confuse the C string routines.


A solution that I think would be quite workable is to just add FREE to the end of the declaration:
DECLARE LIBRARY "foo"
FUNCTION bar$ (BYVAL baz AS LONG) FREE
END DECLARE
Which tells QB64 to call free() on the string returned.

I actually have this half working on the C side of things, but for the life of me I can't work out how to add it to qb64.bas.
QB64 Daily Builds: Windows Linux Mac

  • Print