?
Solved

Pointers for newbies

Posted on 2003-02-25
3
Medium Priority
?
177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

801 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