Board index » delphi » Help! - Pointer Problems In Delphi 2.0

Help! - Pointer Problems In Delphi 2.0

Hello,

 Having problems loading adding an object to a TTreeView node.

PCustomnode is a pointer to a record with a string and an integer;
type PCustomnode = ^MyNode;
       MyNode = record
        sText : string;
        iNum : integer;
.
.
.
var
   tvChildRoot          : TTreeNode;
   cnt                  : integer;
   pNewNode      : PCustomNode;

begin
   cnt := 0;
  {unlock treeview}
   frmMain.tvPubs.readonly := false;

   TreeTable.first;

   {update progress bar}
   pgbUpdate.position := frmSplash.pgbSplash.min;
   pgbUpdate.step := 1;
   pgbUpdate.max := TreeTable.recordcount;

   while not TreeTable.eof do
      begin
         pgbUpdate.stepit;
         Inc(cnt);

{???????????????????????????????????????????????}
the problem is right here with the getmem on my pointer
it runs great and adds the first three children to my root node
no problem.  on the fourth child the assignment of the
text property causes an EInvalidPointer.  
I watched the vars and everything looks great......
I am not sure but this maybe a compiler mem alignment prob
but I'm not sure.
{???????????????????????????????????????????????}

                  getmem(pNewNode,sizeof(PCustomNode));
                  pNewNode^.iNumber_ID := 0;
                  pNewNode^.sText := '';

                  pNewNode^.iNumber_ID :=      
                        TreeTable.fieldbyname('ControlID').asinteger;
                  pNewNode^.sText :=
                TreeTable.fieldbyname('ShortTitle').asstring;

{??????????????????????????????????????????????}

                  tvChildRoot := frmMain.tvPubs.items.addchildobject(

                tvRoot,
                     TreeTable.fieldbyname('ShortTitle').asstring,
                pNewNode);

      freemem(pNewNode,sizeof(PCustomNode));
      TreeTable.next;
    end;
.
.
.
Any help would be greatly appreciated.  This is the same type
of code that came out of the TOutline Help for Delphi 1.0.  Not
in Delphi 2.0 (go figure).  

If anyone has an experience with these Treeviews, I sure would
appreciate some help.

Jeff

 

Re:Help! - Pointer Problems In Delphi 2.0


It looks to me like you're not allocating any space for your string.
You should allocate memory for your string dynamically, or possibly use
a ShortString instead of a string if your stings are not too long.

In article <4mq5jh$...@wrdis02.robins.af.mil>, thom...@usafe16.ramstein.af.mil
says...

Quote
>                  getmem(pNewNode,sizeof(PCustomNode));
>                  pNewNode^.iNumber_ID := 0;
>                  pNewNode^.sText := '';

>                  pNewNode^.iNumber_ID :=      
>                        TreeTable.fieldbyname('ControlID').asinteger;
>                  pNewNode^.sText :=
>                TreeTable.fieldbyname('ShortTitle').asstring;

Re:Help! - Pointer Problems In Delphi 2.0


On 8 May 1996 17:27:02 GMT, mehar...@students.wisc.edu (Mike Harper) wrote:

Quote
>It looks to me like you're not allocating any space for your string.
>You should allocate memory for your string dynamically, or possibly use
>a ShortString instead of a string if your stings are not too long.

You are better off using long strings (AnsiString type) since they are faster
and occupy less memory since they are dynamically allocated. But when you use
them in a record, you have to allocate (or better; initialize) them first so do
this:

Quote
>getmem(pNewNode,sizeof(PCustomNode));
>pNewNode^.iNumber_ID := 0;

Initialize(pNewNode^.sText);

This will initialize the long string - must do this manually within a record.
Since this is a longstring; it's ALWAYS initially empty ('') so no need for
"pNewNode^.sText := '';"

Now when you are finished with this item, and you are ready to FreeMem the node,
you must deallocate the string by using:

Initialize(pNewNode^.sText);

You may now no longer use sText

Note that with long strings that are empty, you may not access there indexes,
example:

sText := ''
ch := sText[1];

will raise an exception - so check that the string contains at least 'index'
characters - use Length(sText)

Sure this helps :),

---
Laurens v. Alphen,  Beach Dreamin' Software, `BeAcH` on IRC
Calslaan 42, 2804 RT Gouda, THE NETHERLANDS, +31 182 539003
email: l...@wirehub.net, website: http://www.wirehub.nl/~lva

"When a lie is told a hundred times, it becomes the truth"

Other Threads