Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6993737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:48:09+00:00 2026-05-27T19:48:09+00:00

Apparently, I don’t understand how constructors work. When my application starts, it automatically or

  • 0

Apparently, I don’t understand how constructors work.

When my application starts, it automatically or when needed executes my procedure that loads all resources – mostly images.

procedure Load;
begin
  AppPath := GetAppPath;
  INI := TInIFile.Create(AppPath + '\Config.ini');
  INI.WriteBool('Application', 'Running', True);
  ResPath := AppPath + '\Resources';
  Top := TPicture.Create;
  TopRight := TPicture.Create;
  Left := TPicture.Create;
  Right := TPicture.Create;
  BottomLeft := TPicture.Create;
  Bottom := TPicture.Create;
  BottomRight := TPicture.Create;

  ...

  //Load the pictures ...LoadFromFile(Skin.ReadString('Skin', ..., ...));

While I was using this application it started to lag a bit, but the OS started to freeze and lag too. I was shocked when I saw my program in Task Manager:

MEM

If I’m right, the application used 600 megs of RAM with page file included. My system has only 1 GB of RAM, so it’s no wonder the OS and almost every program started to lag due the page file usage.

Turned out it was that same procedure that loaded the resources every time I did something. I fixed it with adding a boolean to unit and the procedure canceled when it was set to true (means the resources are loaded).

Loaded: Boolean;
...
if Loaded = False then Load;

I’m not understanding why I had to make that change at all. Why did the constructor allocate memory for already-created objects in the first place?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:48:09+00:00Added an answer on May 27, 2026 at 7:48 pm

    Constructors only allocate memory when the object doesn’t already exist. If the constructor is called on an existing object, then that object is constructed; a new one is not allocated. (What you decide to do inside the constructor is another matter. Those instructions might allocate more memory in addition to whatever the InstanceSize function returns for the class.)

    Your code doesn’t show any constructors being called on existing objects, though. You’re calling constructors on classes, and that allocates a new instance of each class. For example, you construct an INI-file object:

    INI := TInIFile.Create(AppPath + '\Config.ini');
    

    That calls the constructor on TIniFile. It allocates a new TIniFile instance and initializes it. Once that is finished, a reference to that new object is stored in the INI variable. Important to realize is that if INI already held a reference to some other object, that reference is discarded and the new one is stored in INI instead. You’re not calling the TIniFile constructor on the object that’s already referenced by INI. That object is completely independent of the new one being created.

    The INI variable isn’t a TIniFile object. It’s a reference to an object.

    You are apparently calling your Load function multiple times. Each time you call it, you re-run all the object constructors you ran before. You have a memory leak because each time you construct a new set of objects, you store references to them in the same variables you already stored the previous object references in. The previous values are discarded, and there’s no way to get them back, so there’s no way to free them. The way to solve this is as you’ve shown: Make sure you only initialize your program once by keeping track of whether you’ve already loaded.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apparently, this does not work. WHY ??????? I don't want to do all this
I need to change an element's ID using jQuery. Apparently these don't work: jQuery(this).prev(li).attr(id)=newid
I'm iterating through some objects and trying to set some properties that, apparently, don't
Apparently my teacher believes that even if we don't have time to learn stuff
Apparently I don't understand something about binding ListBox controls to data. Here is a
Sorry, but apparently I don't understand chaining enough to figure out this problem... I'm
I thought I understand it, but apparently I don't. Can you help me make
I want to use XHTML5 but apparently they two browsers don't accept the application/xhtml+xml
Session variables are apparently not working for me. I don't know what I'm doing
Apparently, they're confusing. Is that seriously the reason? Can you think of any others?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.