[fpc-pascal] Implementing a true Singleton - Can we decrease the visibility of a method?

Steve Williams stevewilliams at kromestudios.com
Wed Dec 13 22:42:13 CET 2006

Marc Santhoff wrote:
> constructor TSingleton.create;
> begin
>         if not(assigned(s)) then
>         begin
>                 inherited;
>                 (*... do initializations ...*)
>                 s := self;
>                 writeln('creating new singleton');
>         end else begin
>         	self := s; // <-- Danger, Will Robinson!
>         end;
> end;

This is where you lose memory.  TObject.NewInstance is implicitly called 
before the constructor.  It allocates memory for the instance.  Now you 
have lost track of that memory by overwriting Self.  Every time you call 
TSingleton.Create, you lose memory.  Override NewInstance as shown in 
the link I listed previously in this thread for a solution that will not 
lose memory.


This message and its attachments may contain legally privileged or confidential information. This message is intended for the use of the individual or entity to which it is addressed. If you are not the addressee indicated in this message, or the employee or agent responsible for delivering the message to the intended recipient, you may not copy or deliver this message or its attachments to anyone. Rather, you should permanently delete this message and its attachments and kindly notify the sender by reply e-mail. Any content of this message and its attachments, which does not relate to the official business of the sending company must be taken not to have been sent or endorsed by the sending company or any of its related entities. No warranty is made that the e-mail or attachment(s) are free from computer virus or other defect.

More information about the fpc-pascal mailing list