Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Pointers for newbies

Posted on 2003-02-25
3
Medium Priority
?
178 Views
Last Modified: 2010-04-04
I am coming from VB world and there are no pointers like in Pascal, so here is my dilema (although I have read something abot them and have kind of picture what they are).

I use TString to store some information from database. I am using AddObject method and store "Name" as string and TObject(rst.FieldByName('Record_id').AsInteger);

code goes like this
1. I have modal dialog with list box. OnCreate I am calling db:

MyDialogCreate(Sender:TObject);
 var
rst:TADODataSet;
begin
try
  rst:=TADODataSet.Create(nil);
  ...
  for i:=0 to rst.RecordCount-1 do
    begin
    MyListBox.Items.AddObject(rst.FieldByNAme('Name').AsString,TObject(rst.FieldByName('Record_id').AsInteger));
    rst.Next;
    end;
finally
  rst.Free;//HERE IS MY QUESTION
end;
end;

Is rst set to nil after calling rst.Free? I found it still in memory after calling Free.
Is that because I added pointers to those strings in DialogBox?
What happens if I explicitly set it to nil? I still can access Pointer(LongInt(MyListBox...)) which means that Record_id is actually added to the list. If it's pointer that points to my dataset's records, and I kill that dataset, what happens with my Objects in StringList?

Is it safe to do things the way I do in the above procedure?
0
Comment
Question by:bogiboy
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 200 total points
ID: 8020442

1.) rst is not set to nil if you call free. The memory is released, yes, but the pointer to the memory is not cleared. You could use FreeAndNil(), or just set rst to nil after freeing it. Good taste to do it anyways, because accessing a "stale" pointer (pointer directed towards memory that has been freed) can cause all kinds of buggy problems, whereas accessing a nil pointer makes the error obvious.

2.) The objects in your list box are not really objects, but integers cast as TObject. If you were to evaluate the objects[x] of the list box, you would see pointers (for example)

(Record_id = 10) Pointer/Object = $10

Because they are pointers to a constant integer value, the values will be accessible for the lifetime of the list box, regardless of the recordset being freed or not.

Adding actual pointers, strings, interfaces, objects, etc is a different story :-)

3.) Your code looks fine, as long as you always remember to access the objects as integer

i:=Integer(MyListBox.Items.Objects[x]);


Hope this helps,
Russell

0
 
LVL 1

Expert Comment

by:krukmat
ID: 8020873
Just a suggestion:
finally
 rst.close //close it before free memory
 rst.Free;//HERE IS MY QUESTION
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 8021001

The TADODataset (which decends from TCustomADODataSet) already performs a Close on destruction.

destructor TCustomADODataSet.Destroy;
begin
  Destroying;
  Close;
  SetConnection(nil);
  FreeAndNil(FCommand);
  FreeAndNil(FModifiedFields);
  FreeAndNil(FIndexDefs);
  FreeAndNil(FIndexFields);
  FreeAndNil(FMasterDataLink);
  FreeAndNil(FParams);
  inherited Destroy;
end;

Russell
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question