Pascal Ref Confusion - Some questions - Re ListBox.AddItems wants by hcw25539

VIEWS: 16 PAGES: 3

									 Pascal Ref Confusion − Some questions − Re: ListBox.AddItems wants TObject but have Variant

Pascal Ref Confusion − Some questions − Re:
ListBox.AddItems wants TObject but have Variant

Source: http://coding.derkeiler.com/Archive/Delphi/comp.lang.pascal.delphi.misc/2006−12/msg00139.html



      • From: robert <no−spam@xxxxxxxxxxxxxxxxxxxxxxx>
      • Date: Tue, 19 Dec 2006 21:02:44 +0100

alanglloyd@xxxxxxx wrote:


        On Dec 19, 1:52ýpm, robert <no−s...@xxxxxxxxxxxxxxxxxxxxxxx> wrote:

                ListBox.AddItems wants TObject but I have Variant's.
                Seems to force me to create container objects − but where to store them and
                not loose mixup/memleak ...
                How do I best handle this.
                Is it possible to derive a class from 2 base classes in Pascal...?


        This works, but there's most likely a more elegant solution.

        var
        Vi : Variant;

        procedure TForm1.Button1Click(Sender: TObject);
        begin
        Vi := 123456;
        ListBox1.Items.AddObject('Variant', TObject(@Vi));
        end;

        procedure TForm1.ListBox1Click(Sender: TObject);
        begin
        Label1.Caption := IntToStr(PVariant(ListBox1.Items.Objects[0])^);
        end;

        Alan Lloyd



Thanks, this basically works − when "Vi" is held globally or in another container.

And is this a correct mem/free/construction/destruction stepping for dynamic variants? :

// grabbing py−object refs for GUI data collections
var pconn:PVariant

Pascal Ref Confusion − Some questions − Re: ListBox.AddItems wants TObject but have Variant 1
 Pascal Ref Confusion − Some questions − Re: ListBox.AddItems wants TObject but have Variant
..
new(pconn);
pconn^:=pycore.someobject[i].someattr; // causes a py−refcount−inc
SrcList.AddItem(pconn^.name,TObject(pconn));
...
//freeing the list
var pv:PVariant
...
pv:=PVariant(SrcList.Items.Objects[i−1]);
Dispose(pv); // does this do correct destruction of the variant (which holds a py−refcount − see below )
...

It seems to work as basis for a comfortable a Delphi−GUI−bridge to Python datastructures, but is it ok
regarding memleaks, principal errors... ?

Background: I want to comfortable handle and do around mainly with Variant's of the VarPyth module (of the
Python4Delphi Interface, which magically interfaces to Python−Objects and usually does all the
Refcount−Holding etc. − http://www.atug.com/andypatterns/pythonDelphiTalk.htm ) in order do data
handling, algs, db, complex communication etc. in Python (as the Pascal language is somewhat stone age in
that area and a weak standard)
− and use Delphi mainly just for robust and small memory footprint RAD GUI clickering (as alternative to wx
etc) and for few time critical tasks.

I will often need to push those Variant's through Pascal/Delphi GUI item collections (TStrings etc). Is above
method basically optimal? Or is there a more comfortable method to get Variant−Auto−Destruction into the
.Items.Object Containers of Delphi?


Still some basic ref behavior of Delphi/Pascal is quite obscure to me (used to C++ & Python) − thus I fear
mem.leaks and stale refcounts into Python object or invalid refs etc.
I still wonder when exactly refs/objects are created/copied vs skalar copies, when Constructors and
Destructors take place exactly etc. :

TSelDlg.Create(self).ShowModal;
TAboutBox.Create(self).ShowModal; ....
// Is this a valid/good method, when I don't want pre−create all forms during the Application startup?
// Are the form objects automatically new−ed, created and then destructed after the .ShowModal ?
// or how lazy is Pascal? :

Procedure Aboutxxx;
var p:TAboutBox;
pp:^TAboutBox;
begin
p:=TAboutBox.Create(self)
//Application.CreateForm(TAboutBox,p) necessary? and then still .Free necessary ??
p.ShowModal;
p.Destroy; //??
p.Free; //?? only or both?
//Dispose(pp); //??
end;


Pascal Ref Confusion − Some questions − Re: ListBox.AddItems wants TObject but have Variant 2
 Pascal Ref Confusion − Some questions − Re: ListBox.AddItems wants TObject but have Variant
// Other Questions:

var l:TList;

procedure xy;
var po:^Txyobject;
o:Txyobject; // is the constructor already called auto ?
v:variant;
begin
....
v:=pycore.someobject.someattr; o.Create; // this is redundant − or invalid?
o:=Txyobject.Create; // necessary?
new(po); // is o.Create called implicitely or must I always do o.Create(...) extra?
po.Create; po^.Create; // both compile − what is right?
po.v=v;
po^.v=v; // both compile − what is right?
po.Destroy; // necessary or only po.Free or/and Dispose(po);
po.Free;
Dispose(po); // Is the destructor called auto? or must I do po.Free/.Destroy (in addition);
o.v:=v;
l.add(o); // silly? − doesn't keep o alive or? − just a address/pointer?
l.add(po); // keeps − but holy marry: I have to do myself the destruction later?
// how do I set up a variable length list of Variant's or other Objects end; // is o (and o.v's refcount−holder)
disposed and destructed auto? or o.Destroy necessary?

procedure Terminate;
var i:integer;
begin
...
l.clear; // is there any destruction and/or Dispose'ing done auto?
end.



−Robert
.




Pascal Ref Confusion − Some questions − Re: ListBox.AddItems wants TObject but have Variant 3

								
To top