Delphi – Memory management tips

Memory management in Object Pascal is subject to two simple rules: You must destroy every object and memory block you create and allocate, and you must
destroy each object and free each block only once. Object Pascal supports three
types of memory management for dynamic elements, here we can see a list with a quick description:

  1. Every time you create an object, you should also free it. If you fail to do so, the
    memory used by that object won’t be released for other objects, until the program
    terminates.
  2. When you create a component, you can specify an owner component, passing the
    owner to the component constructor. The owner component (often a form or
    data module) becomes responsible for destroying all the objects it owns. In other
    words, when you free the form, it frees all the components it owns. So, if you create
    a component and give it an owner, you don’t have to worry about destroying
    it.
  3. When you allocate memory for strings, dynamic arrays, and objects referenced
    by interface variables (as discussed Chapter 11), Object Pascal automatically frees
    the memory when the reference goes out of scope. You don’t need to free a
    string: when it becomes unreachable, its memory is released. Something similar
    happens under ARC, as we’ll see later on.

memg

In the most simple scenario, on desktop compilers you have to create the temporary
objects you destroy. Any non-temporary object should have an owner, be part of a
collection, or be reachable thought some data structure, which will become responsible
for destroying it in due time.

 

Another problem is that if you call the destructor of an object twice, you get an
error. A destructor is a method that de-allocates an object’s memory. We can write
code for a destructor, generally overriding the default Destroy destructor, to let the
object execute some code before it is destroyed. In your code, of course, you don’t
have to handle memory de-allocation—this is something the runtime library does
for you.

Destroy is a virtual destructor of the TObject class. Most of the classes that require
custom clean-up code when the objects are destroyed override this virtual method.
The reason you should never define a new destructor is that objects are usually
destroyed by calling the Free method, and this method calls the Destroy virtual
destructor (possibly the overridden version) for you.
As I’ve just mentioned, Free is simply a method of the TObject class, inherited by all
other classes. The Free method basically checks whether the current object (Self) is
not nil before calling the Destroy virtual destructor.

 

Be aware that Free does this:

procedure TObject.Free;
 begin
  if Self <> nil then
   Destroy;
 end;

63 risposte a “Delphi – Memory management tips”

  1. Unquestionably imagine that that you stated.
    Your favourite justification appeared to be at the net the easiest thing to be aware of.
    I say to you, I certainly get annoyed whilst folks
    consider issues that they plainly don’t know about. You controlled to
    hit the nail upon the top and also defined out the
    entire thing without having side-effects , people can take a signal.

    Will probably be back to get more. Thank you

  2. I am relocating from my tiny 1 bedroom condominium & Whilst presently fairly clear i involve some help.
    What do I want?

    Hi Ana, Thanks in your initiatives to help us previous week.
    I’m so sorry as I thought I’d released payment. Please
    Speak to me if you are however obtaining difficulties getting this.

    Thanks, Danielle

    “Outstanding company – use them routinely and always get great final results Together with the tradies encouraged. I don’t hassle hunting elsewhere anymore I just use hipages mainly because it’s really easy and they are wonderful to cope with.” –
    Catherine O

    dailymail.co.United kingdom Pastel, clear strains and splashes
    of floral are definitely the get in the day for this yr’s Caulfield Cup in Melbourne.

    I hired Alex on the advice of an excellent friend.
    They did a builders thoroughly clean at our new area, and
    an conclusion of lease thoroughly clean at our previous household.
    Each have been finished extremely perfectly, by having an ho..
    Charlie B Adelaide Fantastic Carpet Cleaning

    We test to keep this record up to date on a regular basis.

    So in this manner you understand we provide the most beneficial finish of lease cleaning Melbourne extensive In keeping
    with actual estates.

    QLD Highlighted happen to be in cleaning domestic and Place
    of work fo13years and possess an eye for detail in what i do and am an experienced cleaner
    and have cu… Time In your case Cleaning

    Number of stop of lease deals. Different clients have various desires.
    So we’ll Present you with an choice to select the most fitted vacate cleaning package
    deal.

    29 Nov 2010 … The last thing you would like to consider when you’re going household is
    definitely the cleaning. But If you would like your bond back again you are going to really have to exert …

    Nick is actually a younger and polite man or woman. I are actually proud of his cleaning solutions to
    the past two months. I am pretty absolutely sure this is going to be a lengthy partnership.
    Lisa Residence Cleaning and Cares

    Hassle-free booking. Timetable a provider to
    the weekdays, weekends and public vacations at the exact
    same price tag.

    This is often only a tutorial and small business rates
    may differ according to exclusive aspects in your
    ask for. Rate selection of similar Work Login in your Oneflare account or Be careful for e-mail to determine quotations because they come in. Watch Career Run by

    The residing region has floorboards and has
    a little balcony to wash.I hope these things also are going to be cleaned.
    A couple of stains on a few of the walls that needs to be earased.
    Common Airtasker cleaning jobs should include: – Just about everywhere in your home:
    Wiping down home furniture and visual surfaces; Mop
    and vacuum floors; Vacant rubbish – Loos: Cleaning showers, bathtub and toilets; – Kitchen area:
    Washing dishes; Please note: This is often an stop-of-lease clear so further attention to depth is
    necessary.

    There was a little bit of miscommunication when it came
    to the cost quoted. This was however, very quickly mounted and Urbanyou honoured
    the extremely initial price quoted (which was under th..
    Yazmeen JoeScrubs

  3. I have been browsing online more than 2 hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. Personally, if all site owners and bloggers made good content as you did, the internet will be a lot more useful than ever before.

  4. Halo, apakah kamu keberatan membiarkan sazya mengwtahui hosting mana yang anda
    gunakan akku telah memasukkan laman kamu kee dalam 3 browser situs
    yyg berbeda dan akku harus mengemukakan bawhwa web inni tidak sedikit dimuat lebih
    segera daripada rata-rata Dapatkah anda merekomendasikan penyedia internet
    hosting yang baik dengan harga yg pantas Terima
    kasih tidak sedrikit saya menghargai itu!

  5. That is really attention-grabbing, You’re an excessively skilled blogger.
    I have joined your feed and sit up for seeking extra of your magnificent post.
    Also, I’ve shared your website in my social networks

  6. Fibre optic cabling will not likely set you back much,
    and they are easy to install. Even with the advanced technology it has taken quite some time for that public to just accept the DVD.

    RCA cables can be common, however, many manufacturers are starting to phase them out in favour of other options.

  7. I usually do not leave a response, however after looking at some of the comments on this page Delphi – Memory management tips – Programming Corner.
    I do have some questions for you if it’s allright. Is it just me
    or do some of these responses look as if they are left by brain dead individuals?
    😛 And, if you are writing at other sites, I’d like to follow you.

    Could you list of all of all your public sites like your twitter feed,
    Facebook page or linkedin profile?

  8. Fantastic items from you, man. I have remember your stuff
    prior to and you are just extremely great. I really like what you’ve received
    here, really like what you are stating and the way in which by which you
    assert it. You’re making it entertaining and you still take care of to keep it sensible.
    I can not wait to read much more from you. That is
    actually a tremendous web site.

  9. Just desire to say your article is as amazing.
    The clarity in your post is just excellent and i could assume you’re an expert on this
    subject. Well with your permission allow me to grab your feed to keep up to date with forthcoming
    post. Thanks a million and please carry on the gratifying
    work.

  10. It is appropriate time to make a few plans for the longer term and it is time to be happy.
    I have read this put up and if I may just I want to recommend you few fascinating things or tips.
    Perhaps you can write next articles regarding this article.

    I wish to learn more issues about it!

  11. Sometimes, doctors don’t invariably aptly handle morning sickness, be responsible for medical problems for your baby.
    Even if vomiting keeps from putting on the weight during your first trimester,
    doctors are generally more worried that you’re staying hydrated rather than explicitly starving yourself.
    Taking soaked cardamom seeds thrice every day works well for reducing stomach ache problems.

  12. Ιt’ѕ a pity you don’t haνe a dnate button! Ι’d m᧐ѕt certainly donate tο һis superb blog!
    І suppose foг now і’ll settle foor book-marking aand adding ʏоur RSS
    feed to my Google account. I loоk foorward to fresh updates аnd wіll alk
    abߋut tgis blog with mу Facebook ɡroup. Chat sοon!

  13. I was curious if you eѵer ϲonsidered changing tһе structure of yoսr site?

    Ӏts vеry well ԝritten; I love what youve got tо say.
    Butt maybe yoᥙ could a littⅼe more in thе way of content so people coulɗ connect witһ it better.
    Youve got an awful lot of text for only hɑving оne օr 2 pictures.
    Ⅿaybe you coulԁ space iit out bettеr?

  14. Greetings from Idaho! I’m bored to death at work so I decided to browse your blog on myy iphone during lunch break.

    I really like thhe info you provgide here and can’t wait to take a look when I get home.
    I’m surprised at how fast your blog loaded on my cell phone ..

    I’m not even using WIFI, just 3G .. Anyways, superb blog!

  15. Hi there, I found your web site via Google even as looking for a comparable topic, your site came up, it looks good.

    I’ve bookmarked it in my google bookmarks.
    Helko there, just changed into aware of your weblog through Google, and locatted that it is realy informative.
    I am going too watch out for brussels. I will appreciate in case you proceed this
    in future. Numerous other people shall be benefited out of your writing.
    Cheers!

  16. Its like you read my mind! You sem to know a lot about this, like you weote
    the book in it or something. I think that you could do
    with some pics to drive the message home a little bit, but other than that,
    this is wonderrful blog. A great read. I’ll
    certainly be back.

  17. You actually make it seem really easy together with your presentation but I find this matter to be really one thng
    which I believe I would by no means understand. It sort of feels too complicated and extremely wide for
    me. I am looming forwadd for your next publish, I’ll try to get tthe hold of it!

  18. If youu are going for finest contenhts like I do, only go to seee this
    website everyday for thee reason that it presents quality contents, thanks

  19. Hey there this is somewhat of off topic butt I was wondering if blogs
    use WYSIWYG editors or if yyou have to manually code with HTML.

    I’m starting a blog soon but have no coding knowleddge so I
    wanted to get advice from someone with experience. Any help would be greatly
    appreciated!

  20. Asking questions are really ood thing if you are not understanding something
    entirely, except this article offers fastidious
    understanding even.

  21. Magnificent items from you, man. I’ve have in mind your stuff
    prior to and you arre simply extremely excellent.
    I really like what you have rceived here, really like what
    you aare sayinbg and the way in which you assert it.
    You make itt enoyable and you continue to care for to keep it
    smart. I can not wait to read far more from you.

    That is really a wonderful site.

  22. This particular post is part of a series. This’s
    the 2nd post and a follow up to the first story of ours titled Display Widgets Plugin Includes Malicious Code to Publish Spam on WP Sites.

    There’s a third posting in this particular series which explains thee way the same spammer influenced a total of
    9 WordPress plugins with a 4.5 year period.

    In this post, we check out who’s behind the invest in as ell as
    corruption of the Display Widgets plugin and a minimum of two other
    popular WordPress plugins.

    As part of my reserarch into the sale of the Display Widgets
    plugin as well as the subsequent spam that appeared in it,
    I’d reached out to Stephanie Wells, the first author of Display Widgetfs who
    sold it. Stephanie goot back to me momentts after I
    hit the publish button on our post.

  23. wholesale jerseys

    I am extremely impressed with your writing skills and also with
    the layout on your weblog. Is this a paid theme or did you modify it yourself?
    Anyway keep up the nice quality writing, it is rare to see a nice blog like this one
    today.INF Cole Figueroa was optioned to Indianapolis on Monday to
    make room on the 25 man roster when 1B Jason Rogers was recalled.
    Rookie Figueroa was hitless in nine at bats this season.

  24. And thanks to the culinary talents of my boy, he can make veggies somewhat.
    Edible! tonight’s dinner is rice, corn on the cob, chicken sausage and cauliflower.
    Possibly some foccacia bread with parmesan cheese.
    He’s adorable when he sleeps n_n.
    sex toys 22052

  25. Hmm it looks like your blog ate my first comment
    (it was extremely long) so I guess I’ll just sum it up what I wrote and say, I’m thoroughly enjoying your blog.

    I too am an aspiring blog writer but I’m still new too everything.

    Do you have any tips and hints for novice blog writers?
    I’d really appreciate it.

  26. I was recommended this website by my cousin. I’m not sure whether this post is written by him as nobody else know such detailed about my problem. You’re amazing! Thanks!

Lascia un commento

Il tuo indirizzo email non sarà pubblicato.