<div dir="auto"><div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Hairy Pixels via fpc-pascal <<a href="mailto:fpc-pascal@lists.freepascal.org">fpc-pascal@lists.freepascal.org</a>> schrieb am Di., 4. Juni 2024, 10:54:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">In the manual it at <a href="https://www.freepascal.org/docs-html/ref/refse38.html" rel="noreferrer noreferrer" target="_blank">https://www.freepascal.org/docs-html/ref/refse38.html</a> 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."<br>
<br>
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.<br>
<br>
class function TDataObject.NewInstance: TObject;<br>
begin<br>
result := TObject(GetMem(InstanceSize));<br>
end;<br></blockquote></div></div><div dir="auto"><br></div><div dir="auto">You also need to call TObject.InitInstance() on the allocated memory (that should probably be mentioned in the documentation...). </div><div dir="auto"><br></div><div dir="auto">Regards, </div><div dir="auto">Sven </div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
</blockquote></div></div></div>