Shop OBEX P1 Docs P2 Docs Learn Events
FlexBASIC garbage collection and null-terminated strings — Parallax Forums

FlexBASIC garbage collection and null-terminated strings

I'm running FlexBASIC, current version, on a P2-EVAL Rev B under Win10. I'm trying to chase down what appears to be memory leak.

Assume that I create a string:
dim myStr$ as string
myStr$ = "This is a test"

Now assume that after creating this string, I decide to wedge a null character (Chr$(0)) between "This" and " is". When the string is ultimately released, does the garbage collection routine understand that it needs to reclaim the whole 15 character string memory space, or does it hit that null at offset 5 and stop there?

Bueller? Anyone? :)

Comments

  • I don't know the answer, but all bets are off when you're poking memory outside of the language keywords.
  • whicker wrote: »
    I don't know the answer, but all bets are off when you're poking memory outside of the language keywords.

    True, that. But when you're trying to make things really scream, and you are faced with limited processor resources, sometimes you need to resort to more unconventional bits of code. In fact, ERSmith works exactly this sort of magical mischief using pointers, ubytes and forced nulls in his "strings.bas" lib included with FlexBASIC. And for good reason: A comparable library sans pointers, forced nulls, etc, runs about 5x slower.

    I have a bad feeling that the GC just looks for that null, finds it, and calls it good. That is one of the problems of using the C-style string convention instead of using a string table that contains not only the address of the string, but a length, and potentially an indicator as to UTF-8, UniCode, etc. The C-style is somewhat faster, but it also has serious limitations (like the inability to have a string contain a null as a valid character unless you want to jump through some hoops).

  • The gc uses a header prepended to the allocated memory to determ8ne its size, IIRC.
  • kwinnkwinn Posts: 8,697
    JRoark wrote: »
    I'm running FlexBASIC, current version, on a P2-EVAL Rev B under Win10. I'm trying to chase down what appears to be memory leak.

    Assume that I create a string:
    dim myStr$ as string
    myStr$ = "This is a test"
    

    Now assume that after creating this string, I decide to wedge a null character (Chr$(0)) between "This" and " is". When the string is ultimately released, does the garbage collection routine understand that it needs to reclaim the whole 15 character string memory space, or does it hit that null at offset 5 and stop there?

    Bueller? Anyone? :)

    It's Bueller's day off so he can't give you an answer, and neither can I. Why not write a little test program to find out.
  • kwinn wrote: »

    It's Bueller's day off so he can't give you an answer, and neither can I. Why not write a little test program to find out.

    Snicker. Ok. I earned that one. :)

    @ersmith Good to see you back, if only briefly. Thanks for the answer!
  • kwinnkwinn Posts: 8,697
    edited 2020-03-19 18:01
    @JRoark, sorry I just couldn't resist ;-)
Sign In or Register to comment.