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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointer assignment disappearing

Status
Not open for further replies.

tjcusick

Programmer
Dec 26, 2006
134
US
I have the following code to fill in a listbox that the user can pick from and it will generate a graph based on the item they pick.

For some reason when they pick the last item it seems to loose its pointer.

I traced through the code and watched as it assigned the correct pointer to the object but later in another part of the code when i pull the pointer the pointer is invalid.

Any Ideas?
Thanks, Tom C

Code:
  GraphData.DBFData.GoTop;
  getmem(tmpPtr, [purple]17[/purple]);
  strcopy(tmpPtr, [teal]'ALL0000000000000'[/teal]);
  Items.AddObject([teal]'All'[/teal], tobject(tmpPtr));
  count:=[purple]0[/purple];
  [b]if[/b] GraphData.dbfData.Seek(tableName) [b]then[/b] [b]begin[/b]
    [b]while[/b] [b]not[/b] GraphData.DBFData.Eof [b]do[/b] [b]begin[/b]
       [b]if[/b] GraphData.DBFData.fieldAsString([teal]'TBL_NAME'[/teal])<>TRIM(tableName) [b]then[/b] 
         break;
       tableId:= GraphData.DBFData.fieldAsString([teal]'TBL_ID'[/teal]);
       Tabledesc:=GraphData.DBFData.fieldAsString([teal]'DESC'[/teal]);
       getmem(tmpPtr, [purple]17[/purple]);
       strcopy(tmpPtr, pchar(tableId));
       Items.AddObject(Tabledesc, tobject(tmpPtr));
       inc(count);
       GraphData.dbfData.Skip([purple]1[/purple]);
    [b]end[/b];
  [b]end[/b];
  len:=count+[purple]1[/purple];
  freemem(tmpptr, len);
  GraphData.DBFData.Close;
  screen.Cursor:=crDefault;

Retrieve code when they select an item from the list...
Code:
  [b]if[/b] [b]not[/b] mDataLoaded [b]then[/b]
  [b]begin[/b]
    InitializeYearData;
    tmpPtr := pchar(cbxType.items.objects[cbxType.itemIndex]);
    [b]if[/b] tmpPtr <> [b]nil[/b] [b]then[/b]
      accountId := strPas(tmpPtr);
    grGetAttendanceData(accountId, intToStr(edYear1.value),
                        mClassType, mYear1, mY1Count);
    grGetAttendanceData(accountId, intToStr(edYear2.value),
                        mClassType, mYear2, mY2Count);
    mDataLoaded := true;
  [b]end[/b];

[navy] for automatic syntax highlighting see faq102-6487
[/navy]
 
seems obvious, you do:

getmem
...
freemem

so basically you deallocate the memory for the pointer.
that it works is just luck (the memory area is not overwritten)

try to work with real objects (not pointers)

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Thanks Daddy that did the trick... Some day i'm gonna learn how to do this programming thing and then i will be dangerous... until then i'll just muddle along...

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top