Recent

Author Topic: Question about "EmptyRecord" in use in the Generics.Collection of FPC 3.2.2  (Read 1603 times)

jamie

  • Hero Member
  • *****
  • Posts: 6735
Just a curious question about why there is an actual real empty record allocated in the "VAR" space of the collection unit and its being used in the THashSet<T>.Add..... as an example.

Line # 946 is where it's declared and it's true, it is absolutely empty!

Looking at line # 2560 is an example of its use?

How does such an item get populated without leaking into memory elsewhere ?
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 16199
  • Censorship about opinions does not belong here.
It was introduced by me as EmptyRecord, and later renamed to TOpaqueData, when commited to system.
It is a reference point to data that is not known or should not be known at compile time.
It can not leak by itself because a simple record is just a var and simple vars don't leak.
Its purpose is twofold (at least twofold):
1. It can be used to describe a reference to a datastructure from another language, e.g. in a shared library, where the library is responsible for its memory management, but the Pascal code needs such a reference to use the interface for that library to be complete.
An example is in Video editing libraries where the Pascal code needs to keep a reference to a frame and pass that reference back in other library calls.
2. In the way that Maciej - the major contributor to the generics.collections library in fpc - uses it: an empty container that needs a pointer allocation later, where the code is responsible to resolve it. That code is usually not exposed.

An example is the TEnumerableWithPointers.
So actually EmptyRecord is now already in system for a couple of years, but with another name.
This is documented here:
https://www.freepascal.org/docs-html/rtl/system/topaquedata.html
https://www.freepascal.org/docs-html/rtl/system/popaquedata.html
and
https://www.freepascal.org/docs-html/rtl/system/opaquepointer.html

It was discussed on the mailing list in the time that Maciej was writing the FreePascal port for the Delphi generics and later commited as a separate entity in system.

In summary it describes a datastructure which structure is not known.
In that sense it is simply a way of describing that the pointers to such construct describe the presence of a datastructure, not its contents. The content and its memory management needs to be managed elsewhere in the code.

« Last Edit: April 21, 2024, 05:39:26 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

jamie

  • Hero Member
  • *****
  • Posts: 6735
I understand what the Empty Record is, but, placing a declaration of it in the VAR section of that unit now allows memory access by means of that reference.

  I can prove that if you wish and watch how the memory pool can get corrupted.

  It's not about the type TEmpyRecord, it's about the actual declaration of it in memory and then that instance is past to the function.

 This gives raise to heap access without nothing after it, as you can see if you were to look, there is no declaration following so any use of this variable is playing with fire.

   Please explain how this cannot happen.

 
The only true wisdom is knowing you know nothing

bytebites

  • Hero Member
  • *****
  • Posts: 685
Talk is cheap.

jamie

  • Hero Member
  • *****
  • Posts: 6735
Talk is cheap.

Thanks for that.

I'll edit my ignorance/ignore list.

Have a good day.


The only true wisdom is knowing you know nothing

bytebites

  • Hero Member
  • *****
  • Posts: 685
LOL, you made my day!

Miss Jamie does that instead of showing some proof.

Thaddy

  • Hero Member
  • *****
  • Posts: 16199
  • Censorship about opinions does not belong here.
It also does not matter: The EmptyRecord/OpaqueData solves real problems. If irresonsible programmers use it in a wrong way, then it is programmer error.
(And such occurence I have demonstrated multiple times on this forum)
Actually, implicitly, he writes that he prefers bad programming.
Maciej used it in the correct way.

Not to mention that its purpose is to provide a reference...
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018