Delphi Memory Management Webinar

Join me this Thursday, Apr 22, 2021 5:00 PM - 6:00 PM CEST for a live Q/A session about Delphi memory management. See you! 



Register for webinar:

https://register.gotowebinar.com/register/474961949731996432


Book information:

https://dalija.prasnikar.info/delphimm 

Comments

  1. Here's some questions in advance for you (which I'll have to catch on the replay (hint, hint) due to timezones).

    What's your thoughts on The Wiert Corner's aversion to using AfterConstruction? (see his blog post 2021/03/23)

    For method parameters what data types do you make const rather than leaving them as a ordinary value parameter? These days I'm using const routinely for strings, interfaces and anonymous methods. I had started doing it for TObject descendants when there was the possibility of my code being run under ARC but I've since removed the consts from those.

    ReplyDelete
    Replies
    1. I forgot about records. I routinely const those as well.

      Delete
    2. AfterConstruction is safe and IMO not used enough. Modifying base class and adding additional virtual DoCreate method is fine, too, if you have base class and you have reasons for not making constructor virtual. But, I would still prefer using it more for configuration than for constructing instances - in other words for final setup after you know all pieces of the object are fully constructed, but that also depends on particular use case. If you need speed, then using AfterConstruction rather than adding new virtual methods is faster.

      The only part where you need to be careful with AfterConstruction is with reference counted classes and triggering reference counting inside AfterConstruction after you called inherited method - that will cause self destruction of the object. See: https://dalijap.blogspot.com/2019/12/self-destructing-object-instance.html

      Also, regardless of the exceptions raised in AfterConstruction, full cleanup will run including BeforeDestruction and Destroy. So that part was not fully correct at least not for newer versions. In Delphi 7 BeforeDestruction is not called, but destructor is. In XE4+ both BeforeDestruction and Destroy are called.

      Anyway, since destructor must be able to handle cleanup of partially constructed objects anyway, BeforeDestruction is necessary only seldom, as anything created there can be cleaned up in Destroy.

      For parameters, I am following same logic you do. I also use it for larger records if I don't need to modify it - it allows compiler to optimize and avoid copying the whole record.

      Delete
    3. I couldn't help myself answering your questions here... but I will mention that topic on the webinar, too :)

      Delete

Post a Comment

Popular posts from this blog

Delphi 12.1 & New Quality Portal Released

Coming in Delphi 12: Disabled Floating-Point Exceptions

Assigning result to a function from asynchronous code