chkaufmann 17 Posted August 29, 2022 A TObject has an InstanceSize of 8 bytes. I searched the internet to understand why. 4 bytes would make sence (pointer to the classtype or whatever). All I find are old postings about this change back in 2009, but is this still valid for the current version? And if the second pointer is available for object locking, can somebody point me to an explanation for this. To be honest, I did not really understand how this works. Currently I use a "Spring.Lock" in all my objects for thread synchronization, but maybe this is not necessary and I could use the built in lock feature of TObject? Christian Share this post Link to post
Remy Lebeau 1398 Posted August 29, 2022 (edited) 31 minutes ago, chkaufmann said: A TObject has an InstanceSize of 8 bytes. I searched the internet to understand why. 4 bytes would make sence (pointer to the classtype or whatever). The extra bytes are for a hidden pointer to a System.TMonitor instance (not to be confused with Vcl.Forms.TMonitor). Every TObject instance has an optional TMonitor instance associated with it. The actual TMonitor instance is created the 1st time it is accessed, but the parent TObject's InstanceSize includes space for a pointer to the TMonitor instance. Quote All I find are old postings about this change back in 2009, but is this still valid for the current version? AFAIK, yes. Quote And if the second pointer is available for object locking, can somebody point me to an explanation for this. Read the TMonitor documentation, and also see What is TMonitor in Delphi System unit good for? Quote Currently I use a "Spring.Lock" in all my objects for thread synchronization, but maybe this is not necessary and I could use the built in lock feature of TObject? You could, yes. You would simply call TMonitor.Enter(obj) and TMonitor.Exit(obj) where needed. Edited August 29, 2022 by Remy Lebeau 1 Share this post Link to post
chkaufmann 17 Posted September 6, 2022 On 8/29/2022 at 7:34 PM, Remy Lebeau said: The extra bytes are for a hidden pointer to a System.TMonitor instance (not to be confused with Vcl.Forms.TMonitor). Every TObject instance has an optional TMonitor instance associated with it. The actual TMonitor instance is created the 1st time it is accessed, but the parent TObject's InstanceSize includes space for a pointer to the TMonitor instance. AFAIK, yes. Read the TMonitor documentation, and also see What is TMonitor in Delphi System unit good for? You could, yes. You would simply call TMonitor.Enter(obj) and TMonitor.Exit(obj) where needed. When I read https://www.delphitools.info/2013/06/06/tmonitor-vs-trtlcriticalsection/, then I should not use TMonitor. Is this still the case? Is there another simpler solution to lock an object in order to avoid that multiple threads change an internal value. Locktime will be very short and conflicts probably happen very rarely. Share this post Link to post
chkaufmann 17 Posted September 6, 2022 And what about using a TLightweightMREW for each of my objects compared to TMonitor and Spring.Lock? I probably need this to have a reentrant version of the lock: https://www.thedelphigeek.com/2021/02/readers-writ-47358-48721-45511-46172.html Christian Share this post Link to post
Fr0sT.Brutal 900 Posted September 19, 2022 On 9/6/2022 at 4:02 PM, chkaufmann said: https://www.delphitools.info/2013/06/06/tmonitor-vs-trtlcriticalsection/ Haven't you noticed that: Quote Edit 2013-08-24: looks like the issue discussed here should be resolved in XE5 Share this post Link to post