Here's my string reference counting bug of the day. Strings passed as parameters can be used after free if the variable is modified between the string being pushed onto the stack and the function being called (e.g. by another function constructing a parameter).

Here's my string reference counting bug of the day. Strings passed as parameters can be used after free if the variable is modified between the string being pushed onto the stack and the function being called (e.g. by another function constructing a parameter).

Or am I just doing bad things with strings?

https://quality.embarcadero.com/browse/RSP-20457
https://quality.embarcadero.com/browse/RSP-20457

Comments

  1. Nice bug report. Thorough and clear. Thankyou.

    ReplyDelete
  2. Another reason to use const with string parameters whenever possible?

    ReplyDelete
  3. Not surprised by this bug. Reference counting along with memory management thereof can get quite complex.

    This is why I never rely on this sort of feature. It's important to know your own code and explicitly manage memory yourself.

    ReplyDelete
  4. Jennifer Powell Really, you don't use Delphi strings at all?

    ReplyDelete
  5. I definitely use strings but certainly don't rely on any reference counting features to manage them. Otherwise I would have likely discovered the garbage collection bug you have noted.

    For confidence, I do my own garbage collection.

    ReplyDelete
  6. Jennifer Powell out of interest, how do you do your own memory management of strings?

    ReplyDelete
  7. Funny, I just posted the rant below about a "another" compiler, where I had to call SetLength(s, length(s)); to keep the string hanging around between two lines of code. Kinda makes me question using anything that was reference counted by anything but me. My rant:

    s := 'ARC IS FUBAR';
    SetLength(s, length(s)); // Really???
    // !!! I CANNOT BELIEVE I HAD TO DO THAT !!!
    ReadAChar(@s[2], 1);
    // I am doing doing other things with s...
    // LIKE PRAYING FOR IT!!!

    I must admit, I hate ARC. It leads to sloppy programming practices. But having to SetLength(s, length(s)) is ridicules. A really really good compiler might even optimize it out. I would.

    ReplyDelete

Post a Comment