Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AddChildObject problems

Status
Not open for further replies.

tjc240e

Technical User
Nov 12, 2004
133
US
I am getting an access violation error in the line DataPtr^.PID:=ParentID; and im not sure what is happening. I was following the tutorials for setting up pointers to use in the TreeNode and TreeView.

If i remove that line and replace the AddChildObject with AddChild it works without a problem but I need the pointer for future references.

Any Suggestions?

I am using Delphi 7.

Code:
type 
   PData = ^TNData;
   TNData = Record
      PID: Integer; 
      SID: Integer; 
   end;
...
procedure MakeTheTree(udfName, xName:String);
var 
  xCHAct: string;
  CSREFTBL: TCBTable;
  DataPtr: PData;
  ParentID: Integer;
begin  
  with AttendanceOptionsFrm do
  begin          
    SelectedNode:=AttList.Items.Add(nil, udfName);
    AttList.Items.AddChild( SelectedNode, '<All '+udfName+'>');
    CSREFTBL.Open;
    CSREFTBL.GoTop;
    IF xName='X' then
      CSREFTBL.SetFilter('TBL_NAME="X"');
    IF xName='Y' then
      CSREFTBL.SetFilter('TBL_NAME="Y"');
    IF xName='Z' then
      CSREFTBL.SetFilter('TBL_NAME="Z"');
    WHILE not CSREFTBL.Eof do Begin
      if CSREFTBL.FieldAsString('TBL_NAME')=xName then BEGIN
        xCHAct := CSREFTBL.FieldAsString('DESC');
        ParentID:=CSREFTBL.FieldAsInteger('TBL_ID');
        DataPtr^.PID:=ParentID;
        AttList.Items.AddChildObject(SelectedNode, xCHAct, DataPtr);
      END;
      CSREFTBL.Skip(1);
    ENd;
    CSREFTBL.Close;
  end;
end;
 
When using record pointers, you must allocate memory using New and Dispose. eg.

Code:
[navy]// snip[/navy]
New(DataPtr);
DataPtr^.PID := ParentID;
AttList.Items.AddChildObject(SelectedNode, xCHAct, DataPtr);

You must pair each call to New with a call to Dispose, otherwise you'll leak memory. So, when it comes to removing any child objects from AttList.Items, you must first assign a PData variable to the object, then call
Code:
[navy]// PData := ...  replace ... with appropriate reference[/navy]
Dispose(PData);
to free the allocated memory.
 
Griffyn; so your saying that i dispose of the PData (pointer array)? Do i do that at the very end of the procedure or do i do that end of the whole program?
 
Ok now i know how to retrieve the text from what ever item is selecting in my AttList but how to i retrieve the pointer?

There is nothing that pops up in the listing that talks about pointers?

The index tells me what number the item is indexed at but that is not the number i need.

Any suggestions?

Code:
var
  sNode, xTblId: String;
begin
    ListAttendEvents.Clear;
    SNode:=AttList.Selected.Text;
    ListAttendEvents.Items.Add(SNode);
    STR(attlist.Selected.index, xtblid);
    ListAttendEvents.Items.Add(xtblid);
end;
 
For anyone who might be interested i found the solution on a different website...

Thanks to all who helped me getting it this far...

Anywho the following code is what i did to get the results:

Code:
var
  sNode, x, xTblId: String;
begin
    ListAttendEvents.Clear;
    SNode:=AttList.Selected.Text;
    ListAttendEvents.Items.Add(SNode);
    xTblId:=PData(AttList.Selected.Data)^.Att_Type;
    if PData(Attlist.selected.Data)<> nil then
      x:=PData(AttList.Selected.Data)^.PID
    else
      x:='All';
    ListAttendEvents.Items.Add(x);
    ListAttendEvents.Items.Add(xtblid);
end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top