2

In my framework I have a DataSource1 instance that was dropped in design time on a Form.
At some point at run time I need to "convert" it to a reference to another DataSource2 (on a Data Module).

Is simply setting:

DataSource1 := DataSource2;

Enough to make DataSource1 a reference of DataSource2? it appears that DataSource1 is not being destroyed at this point - It is destroyed when the Owner of DataSource2 is destroyed, And that there are in fact two instances of TDataSource.

Or do I need to explicitly free DataSource1 first?

DataSource1.Free;
DataSource1 := DataSource2;

What is the correct way? (Besides declaring DataSource1 as a reference in the first place)

2
  • 1
    In the first place could be useful to know what is the purpose of this. Are you're doing this e.g. because you're having some "background" dataset working and after its work is done, you're populating its data by "reconnecting" data sources this way ? If so, then I would rather re-assign data source's dataset. Commented Jan 12, 2014 at 12:26
  • @TLama, I see what mean. the question is not so much related to DB design. the purpose is a bit complicated to explain: we have a base class form that exposes a centralized TDataSource to it's underlying db-aware controls. (it's persistent by default) but some times it should be referenced to another DataSource. I know I could set/change the DataSet in that persistent DataSource (and that is the correct way), but I was just curious to understand that instance referencing. Commented Jan 12, 2014 at 12:42

1 Answer 1

4

When you declare a variable to be of a type that inherits from TObject, you are actually declaring a pointer.

When you call a constructor, you are creating an instance. The constructor returns a pointer to that instance. You typically assign that pointer to a variable like this:

Obj1 := TMyClass.Create;

You can make a second variable point to, or refer to, the instance with simple assignment:

Obj2 := Obj1;

The object is destroyed by calling Free:

Obj1.Free;

At this point Obj2 refers to an object that no longer exists. We say that Obj2 is a stale reference.

In your case you need to free the first object:

Sign up to request clarification or add additional context in comments.

15 Comments

@Zig - What you call "persistent DataSource1" is just a field variable of some data module, a pointer. After DataSource1:=DataSource2, you no longer have a reference to the object that was referred by the left side variable/pointer. Hence you have to free it before you loose the reference, or leave it to the framework.
DataSource1 is not an instance. It is a pointer to an instance. When you write DataSource1 := ... you do nothing whatsoever to the instance that DataSource1 originally referred to. The question is I suppose, do you understand how pointers work?
David, in your answer there is a memory leak regarding original Obj1. I guess it would be good to tell it explicitly.
@zigiz there is a variable name (in compile time, or in case variable is a field of an object like of a form, it might also exist in runtime via RTTI), and there is the value of that var (a pointer) somewhere in RAM, and there is a pointer itself in another place of memory, and - if the object was derived from TComponent - it also has a name as it's properties. In a proper program those 4 entities are usually concerted together, but they still are different entities of their own kinds
@Arioch There is no memory leak. One object is created. That object is freed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.