Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2022-10

Variants with classes and records

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Hairy Pixels

Is it possible to assign a class to a variant? I tried and get a crash. If not, why not? It’s just a pointer so I would expect it to work.

I also see a “varRecord” constant (in varianth.inc) which suggests you could assign a record to a variant but I get an error trying to do this, and honestly that makes sense so I’m curios what this constant is for.

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Hairy Pixels
Hairy Pixels wrote
Is it possible to assign a class to a variant? I tried and get a crash. If not, why not? It’s just a pointer so I would expect it to work.

There is no field that would hold a class reference and no type value that would differentiate it, thus the code handling variants would not be able to finalize (aka free) a class correctly. You can of course always use a custom type and handle that yourself. *shrugs*

I also see a “varRecord” constant (in varianth.inc) which suggests you could assign a record to a variant but I get an error trying to do this, and honestly that makes sense so I’m curios what this constant is for.

The underlying TVarData record stems from Microsoft's COM ( https://learn.microsoft.com/en-us/windows/win32/api/oaidl/ns-oaidl-variant ) where there is also a record field, but neither Delphi nor FPC ever supported that.

Regards,
Sven

Reply by email

Hairy Pixels
to Sven Barth
Show quoted text 6 lines
On Oct 6, 2022, at 12:45 PM, Sven Barth wrote: Am 05.10.2022 um 14:16 schrieb Hairy Pixels via fpc-pascal: > Is it possible to assign a class to a variant? I tried and get a crash. If not, why not? It’s just a pointer so I would expect it to work. There is no field that would hold a class reference and no type value that would differentiate it, thus the code handling variants would not be able to finalize (aka free) a class correctly. You can of course always use a custom type and handle that yourself. *shrugs*

Oh so the problem is variant thinks it should try to manage memory for you? I tried doing:

  v: variant;
begin
  v := GetMem(100);

And that crashed also. I thought it would be ok to just hold the reference but not claim ownership over it.

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Hairy Pixels

Hairy Pixels schrieb am Do., 6. Okt. 2022, 13:49:

Show quoted text 24 lines
> On Oct 6, 2022, at 12:45 PM, Sven Barth wrote: > > Am 05.10.2022 um 14:16 schrieb Hairy Pixels via fpc-pascal: >> Is it possible to assign a class to a variant? I tried and get a crash. If not, why not? It’s just a pointer so I would expect it to work. > > There is no field that would hold a class reference and no type value that would differentiate it, thus the code handling variants would not be able to finalize (aka free) a class correctly. You can of course always use a custom type and handle that yourself. *shrugs* Oh so the problem is variant thinks it should try to manage memory for you? I tried doing: v: variant; begin v := GetMem(100); And that crashed also. I thought it would be ok to just hold the reference but not claim ownership over it.

Variants count as managed types and they can contain interface references and strings. If you use a custom type you can do as you please, but the internal types that can be assigned to a Variant *are* managed accordingly (if the types themselves are managed).

Regards,
Sven

Reply by email

Marco van de Voort
to Sven Barth
Sven Barth wrote 10 lines
And that crashed also. I thought it would be ok to just hold the reference but not claim ownership over it. Variants count as managed types and they can contain interface references and strings. If you use a custom type you can do as you please, but the internal types that can be assigned to a Variant *are* managed accordingly (if the types themselves are managed).

(IIRC in case of native types there is another pointer in the variant which is an interface for something that manages the record.  But it has been a while)

Reply by email