Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was typing an answer as David Heffernan just beat me to it. Still while at it i would like to add;</p> <p>Realize that in your first code example you are using a record (TSprite) to hold an object (TImage). TImage is a visual component actually, but ultimately it descends from TObject.</p> <p>A <strong>record</strong> is like most variables (such as Integer) in that it does not have to be instantiated and can be copied at will.</p> <p>An <strong>object</strong> however is a pointer to an instance, and must be created/destroyed by following these steps</p> <pre><code>rec.Image := TImage.Create(nil); // do other things.. rec.Image.Free; </code></pre> <p>And thus may result in memory leaks or Access Violation errors if not done properly. (for example while copying a TSprite..) There is a lot of things that can go wrong in this setup, so therefore i say;</p> <h3>Using objects in records can get tricky. Consider keeping objects in objects:</h3> <p>A simple solution (if you indeed want to keep an instance or pointer reference of TImage inside a TSprite) would be to make TSprite also an object. It could keep track of of creating/destroying automatically by using its constructor and destructor:</p> <pre><code>TSpriteObject = class(TObject) public Image : TImage; constructor Create; destructor Destroy; override; end; </code></pre> <p>and its implemenation:</p> <pre><code>constructor TSpriteObject.Create; begin Image := TImage.Create(nil); // ^ TImage is a component and expects an Owner component that would also // destroy it, so we use a nil value to disable that behavior. end; destructor TSpriteObject.Destroy; begin Image.Free; end; </code></pre> <p>You can then have a TObjectList keep track of many instances of TSprite, and it'll destroy any TSprite (which destroys its TImage) when the list is cleared or destroyed.</p> <p>(this is my first ever stackoverflow post attempt, please bear with me while i find out what i'm doing wrong here)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload