FastMM5 icon indicating copy to clipboard operation
FastMM5 copied to clipboard

nil in LPShortString in BlockContentTypeToTextBuffer

Open Nashev opened this issue 1 year ago • 4 comments

I have tried to use FastMM_LogStateToFile which have a nested calls of BlockContentTypeToTextBuffer procedure in my code for explore some memory leaks, and have faced with situation, when in LPShortString after line

LPShortString := PShortString(PPointer(PByte(LClass) + vmtClassName)^); 

was nil and further manipulation with it fails and prevent to write a memory usage dump.

I do not remember now, what the object leads to this misbehaviour, but when I added a code like

      if LPShortString = nil then
        Result := AppendTextToBuffer('<nil class name>', Length('<nil class name>'), Result, APTargetBufferEnd)
      else
        begin

it wrote one line about this nil, and all others data was very useful for my purposes.

Nashev avatar May 06 '24 10:05 Nashev

Is it possible that the block content was modified after it was detected as a class? (Perhaps by another thread?) Unfortunately it's risky to try and log the content of a memory block while other threads may be manipulating those same blocks.

I could add that nil check, but there would still be a race condition - another thread could very well free the entire block immediately after the nil check, which would still lead to a crash.

pleriche avatar May 06 '24 13:05 pleriche

May be, but size and count of that memory blocks was stable across the long time.

Nashev avatar May 06 '24 14:05 Nashev

Anyway, I expect FastMM_LogStateToFile to work in enough independently, relative to other memory management routines. I would like to think this nil is from some RTTI-storing issue, not from race conditions.

Nashev avatar May 06 '24 14:05 Nashev

I have recently added some additional sanity checks to FastMM_DetectClassInstance that I believe may solve this issue as well.

pleriche avatar Oct 04 '24 11:10 pleriche

Just for info, You have mean the https://github.com/pleriche/FastMM5/commit/58d018c788f22d27a861cdb3b8a22c655357eb96 commit.

Also may be the next https://github.com/pleriche/FastMM5/commit/d8b49057b63350f6d654a9e317f589c2649d7ee9 is a little bit related.

I'm not sure this fixes my issue, but yes, it may be.

Ilia-Nenashev-devX avatar May 13 '25 09:05 Ilia-Nenashev-devX