Board index » delphi » Problems with swaping content between objects

Problems with swaping content between objects

Hi Guys,
I have some problems with the following situation---

TYPE TEngineOBj=class(TObject)
TYPE TShip=class(TEngineObj);
TYPE TShip2=class(TEngineObj);

This are the objects I used! So the sesond and third are from TEngineObj.
Now I am using an array of TEngineObj

VAR OBJ:ARRAY[1..3] OF TEngineObj;

Then my program fills in some values into this array like:
BEGIN
    OBJ[1]:=TShip1.Create;
    OBJ[2]:=TShip2.Create;
END;

So now the problems comes ;-)
In one part of the program I have to move the content the whole OBJ[2] to
the first position in the array. The whole Object with all content, like the put out of
the second place and overwrites the first.

BEGIN
    OBJ[1]:=Obj[2];
END;
That was my try! In fact, if this arent objects it could be successful...
Instead the first object get the RAM adresse of the second and if I write
something to the first it also will be written into the second. And I dont want this
because I have to kill the second object after this copy and on this way I also will
kill the second...

Hope you understand what I try to explain and also hope that heres someone who
can help me ;-)

ThanX in advise!
Sincely,
    Florian
--
"Please excuse my terrible english! I know thats cruel, so please dont laugh ;-)"

 

Re:Problems with swaping content between objects


Quote
"Florian Sievert" <Phob...@T-Online.de> wrote in message

news:8es62h$u7c$1@news03.btx.dtag.de...
Hi Guys,
I have some problems with the following situation---

TYPE TEngineOBj=class(TObject)
TYPE TShip=class(TEngineObj);
TYPE TShip2=class(TEngineObj);

This are the objects I used! So the sesond and third are from TEngineObj.
Now I am using an array of TEngineObj

VAR OBJ:ARRAY[1..3] OF TEngineObj;

Then my program fills in some values into this array like:
BEGIN
    OBJ[1]:=TShip1.Create;
    OBJ[2]:=TShip2.Create;
END;

So now the problems comes ;-)
In one part of the program I have to move the content the whole OBJ[2] to
the first position in the array. The whole Object with all content, like the
put out of
the second place and overwrites the first.

BEGIN
    OBJ[1]:=Obj[2];
END;
That was my try! In fact, if this arent objects it could be successful...
Instead the first object get the RAM adresse of the second and if I write
something to the first it also will be written into the second. And I dont
want this
because I have to kill the second object after this copy and on this way I
also will
kill the second...

Hope you understand what I try to explain and also hope that heres someone
who
can help me ;-)

As you found out, object variables are pointers to actual object instances
so Obj [x] := Obj [n] simply sets entry x to point to the same instance as
entry n. If you want to copy object instances you need to do what the VCL
does (see tPersistent) and create a method to do the copying, see below.
However, in the example you cite above you indicate that you free the
instance being copied after the copy so doing 'obj [1] := obj [2]; obj [2]
:= nil;' may accomplish what you want (no need to do obj [2].free
afterwards).

Type

tEngineObj = class (tObject)
    . . .
    public
        procedure Assign (src : tEngineObj); virtual;
    . . .
    end;

tShip = class (tEngineObj)
    . . .
    public
        procedure Assign (src : tEngineObj); override;
    . . .
    end;

procedure tEngineObj.Assign (src : tEngineObj);

begin
// copy src data to self
end;

procedure tShip.Assign (src : tEngineObj);

begin
inherited Assign (src);
// copy tShip specific src data to self
end;

ThanX in advise!
Sincely,
    Florian
--
"Please excuse my terrible english! I know thats cruel, so please dont
laugh ;-)"

Re:Problems with swaping content between objects


In article <RvhQ4.35769$Xk2.131...@tor-nn1.netcom.ca>, "Bruce Roberts"

Quote
<no.junk.please....@attcanada.net> writes:
>However, in the example you cite above you indicate that you free the
>instance being copied after the copy so doing 'obj [1] := obj [2]; obj [2]
>:= nil;' may accomplish what you want (no need to do obj [2].free
>afterwards).

But you've got to decide what you want to do to Obj[1]. If you want to keep it
then first assign the reference to another TShip1 or TEngineObj variable. If
you want to get rid of it, then first call Obj[1].Free. THEN do Obj[1] :=
Obj[2]; Obj[2] ;= nil;

Otherwise Obj[1] becomes a memory leak because it has been allocated by the
Create constructor, but you have no reference to the memory (or object) to be
able to free it.

It's a somewhat similar approach if you wanted to swap the two, you have to do
it via a temporary third variable to hold the object reference.

Alan Lloyd
alangll...@aol.com

Re:Problems with swaping content between objects


Quote
> However, in the example you cite above you indicate that you free the
> instance being copied after the copy so doing

Shouldn't obj [1] be freed first?
obj [1].free
and then

'obj [1] := obj [2]; obj [2]

Quote
> := nil;' may accomplish what you want (no need to do obj [2].free
> afterwards).

regards hanford

Re:Problems with swaping content between objects


Quote
"Hanford Carr" <l...@mediaone.net> wrote in message

news:3911DAF9.C8BBFAB7@mediaone.net...

Quote
> > However, in the example you cite above you indicate that you free the
> > instance being copied after the copy so doing

> Shouldn't obj [1] be freed first?

Good point.
Quote
> obj [1].free
> and then

> 'obj [1] := obj [2]; obj [2]
> > := nil;' may accomplish what you want (no need to do obj [2].free
> > afterwards).

> regards hanford

Re:Problems with swaping content between objects


"Bruce Roberts" <no.junk.please....@attcanada.net> schrieb im Newsbeitrag news:RvhQ4.35769$Xk2.131571@tor-nn1.netcom.ca...

Quote

> "Florian Sievert" <Phob...@T-Online.de> wrote in message
> news:8es62h$u7c$1@news03.btx.dtag.de...
[my question]
> As you found out, object variables are pointers to actual object instances
> so Obj [x] := Obj [n] simply sets entry x to point to the same instance as
> entry n. If you want to copy object instances you need to do what the VCL
> does (see tPersistent) and create a method to do the copying, see below.
> However, in the example you cite above you indicate that you free the
> instance being copied after the copy so doing 'obj [1] := obj [2]; obj [2]
> := nil;' may accomplish what you want (no need to do obj [2].free
> afterwards).

ThanX for the fast help! Yes, your example helped me out of this trouble! All works
fine now. Dont now even, why I havent such a idea like assign before... Easy way is
to hard to find :-]

And also thanks to the other who answered. I also forget the release the first Object,
maybe helped me to save some ram ;-)

Have a nice weekend!
    Florian

"Please excuse my terrible english! I know thats cruel, so please dont laugh ;-)"

Other Threads