[fpc-pascal] Custom NewInstance allocator

Olivier Sannier olivier at obones.com
Tue Jun 4 10:58:50 CEST 2024


Could it be because you do not zero out the allocated memory?


Le 04/06/2024 à 10꞉54, Hairy Pixels via fpc-pascal a écrit :
> In the manual it athttps://www.freepascal.org/docs-html/ref/refse38.html  it says "Calling the constructor will provoke a call to the virtual class method NewInstance, which, in its default implementation, calls GetMem, to allocate enough space to hold the class instance data, and then zeroes out the memory."
>
> I'm trying this like below but it crashes. Is this correct? The fact NewInstance returns TObject instead of Pointer doesn't make sense to me and suggests this isn't correct.
>
>    class function TDataObject.NewInstance: TObject;
>    begin
>      result := TObject(GetMem(InstanceSize));
>    end;
>
> Regards,
> 	Ryan Joseph
>
> _______________________________________________
> fpc-pascal maillist  -fpc-pascal at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20240604/9b511706/attachment.htm>


More information about the fpc-pascal mailing list