Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TRttiInfo.DynArrayItemSize get 0 when no elType is present #82

Closed
mksjgj opened this issue Apr 4, 2022 · 5 comments
Closed

TRttiInfo.DynArrayItemSize get 0 when no elType is present #82

mksjgj opened this issue Apr 4, 2022 · 5 comments

Comments

@mksjgj
Copy link

mksjgj commented Apr 4, 2022

function TRttiInfo.DynArrayItemSize: PtrInt;
begin
if DynArrayItemType(result) = nil then
result := 0; --> this is an error !!! Item size is independent of elType
end;

function TRttiInfo.DynArrayItemType(out aDataSize: PtrInt): PRttiInfo;
begin
with GetTypeData(@self)^ do
begin
aDataSize := elSize;
result := pointer(elType); //elType is nil if type does not require cleanup
if result <> nil then
result := PPointer(result)^;
end;
end;

@synopse
Copy link
Owner

synopse commented Apr 4, 2022

This function does what it is documented to do: " get the dynamic array type information of the stored item"
So if there is no stored item, elType is nil.

What do you want to achieve?
If you want to access the RTTI as mORMot does, use TRttiCustom and its properties, e.g. Cache.ItemSize.

Please use the forum to discuss this.

@synopse synopse closed this as completed Apr 4, 2022
@mksjgj
Copy link
Author

mksjgj commented Apr 5, 2022

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
System.SysUtils,
mormot.core.rtti;

type
PSimpleLocator = ^TSimpleLocator;
TSimpleLocator = packed record
Id:Int32;
Pos: Int32;
end;
TSimpleLocatorArray = array of TSimpleLocator;

var
FXLocArrTyp :PRttiInfo;
sz:Integer;
begin
FXLocArrTyp := TypeInfo(TSimpleLocatorArray);

sz := FXLocArrTyp.DynArrayItemSize;
WriteLn('DynArrayItemSize: ItemSize is ' + sz.ToString); //DynArrayItemSize: ItemSize is 0

FXLocArrTyp.DynArrayItemType(sz);
WriteLn('DynArrayItemType: ItemSize is ' + sz.ToString); //DynArrayItemType: ItemSize is 8

WriteLn('You can see, something is wrong!');
end.

@synopse synopse reopened this Apr 5, 2022
@synopse
Copy link
Owner

synopse commented Apr 5, 2022

I guess you are using Delphi, not FPC?

@mksjgj
Copy link
Author

mksjgj commented Apr 5, 2022

yes delphi 10.3.3

@synopse
Copy link
Owner

synopse commented Apr 5, 2022

Should be fixed now.

Thanks for the feedback.

But anyway, you should better use TRttiCustom and its properties, e.g. Cache.ItemSize, rather than those lower level functions.

@synopse synopse closed this as completed Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants