[fpc-pascal] Custom NewInstance allocator
In the manual it at https://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@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Custom NewInstance allocator
Hairy Pixels via fpc-pascal schrieb am Di., 4. Juni 2024, 10:54: > In the manual it at https://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; > You also need to call TObject.InitInstance() on the allocated memory (that should probably be mentioned in the documentation...). Regards, Sven > ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Custom NewInstance allocator
Thanks it's working now. The docs at https://www.freepascal.org/docs-html/rtl/system/tobject.newinstance.html confused me also. It says: " If the memory was allocated, the class will be initialized by a call to InitInstance" The text "will be" is what confused me. This implies to me it will be done by Object somewhere higher up in the chain. It should read: "If memory was allocated it must be initialized by a call to InitInstance" > On Jun 4, 2024, at 7:06 PM, Sven Barth via fpc-pascal > wrote: > > Hairy Pixels via fpc-pascal schrieb am Di., > 4. Juni 2024, 10:54: > In the manual it at https://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; > > You also need to call TObject.InitInstance() on the allocated memory (that > should probably be mentioned in the documentation...). > > Regards, > Sven > ___ > fpc-pascal maillist - fpc-pascal@lists.freepascal.org > https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal Regards, Ryan Joseph ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal