Recent

Author Topic: [Solved] StringGrid, a cell with a deleted TObject gives an access violation.  (Read 560 times)

Hansvb

  • Hero Member
  • *****
  • Posts: 773
Hi,

In a stringgrid, I add an Object with some data to each cell. If I click on a cell with data, the contents of the object is shown in a TEdit. If I then delete the contents of the cell, I also remove the object of this cell. At least that is the intention. When I then click on the cell from which the object was removed, I get an error (access violation). I have put in a simplified project which reproduces this.
If you click on a cell with the right mouse button you get a popup with "Delete". If you click on this, the cell will be deleted and the object will be deleted. If you then click on the empty cell again, you will get an access violation.

Is my check if there is an object attached to the cell wrong? Or is deleting the object wrong?
« Last Edit: March 22, 2025, 10:03:50 am by Hansvb »

bytebites

  • Hero Member
  • *****
  • Posts: 709
Re: StringGrid, a cell with a deleted TObject gives an access violation.
« Reply #1 on: March 19, 2025, 10:29:19 am »
There is dangling pointer after dispose and it needs to be cleared
Code: Pascal  [Select][+][-]
  1. StringGrid1.Objects[aCol,aRow]:=nil;

Zvoni

  • Hero Member
  • *****
  • Posts: 2961
Re: StringGrid, a cell with a deleted TObject gives an access violation.
« Reply #2 on: March 19, 2025, 10:29:56 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.MenuItem1Click(Sender: TObject);
  2. begin
  3.   StringGrid1.Cells[FCol, FRow]:= '';
  4.  
  5.   Dispose(PtrI]"]>Blockedbject(StringGrid1.Objects[FCol,FRow]));
  6.   StringGrid1.Objects[FCol,FRow]:=Nil;  //<-- HERE
  7. end;          
  8.  
  9. procedure TForm1.StringGrid1SelectCell(Sender: TObject; aCol, aRow: Integer;
  10.   var CanSelect: Boolean);
  11. var
  12.   p: PtrI]"]>Blockedbject;
  13. begin
  14.   FCol:= aCol; // Used with Delete.
  15.   FRow:= aRow; // Used with Delete.
  16.  
  17.  if PtrI]"]>Blockedbject(StringGrid1.Objects[acol,aRow]) <> nil then begin
  18.     p := PtrI]"]>Blockedbject(StringGrid1.Objects[acol,aRow]);  // <<------ this goes wrong when the object is deleted.
  19.     Edit1.Text:= p^.Name; // <-- access violation when delete is used.
  20.   Else
  21.     Edit1.Text:='';
  22.   end;                
  23. end;                              
  24.  
  25.  
  26.  

Yes, you dispose of the Data, but it doesn't "nil" the Pointer of Objects[Fcol,Frow]

EDIT: Bytebites beat me to it

EDIT: There is something weird with the Forum and your var-name "PtrI]"]>Blockedbject"...

Testing
Quote
PtrI]"]>Blockedbject
« Last Edit: March 19, 2025, 10:33:07 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

cdbc

  • Hero Member
  • *****
  • Posts: 2067
    • http://www.cdbc.dk
Re: StringGrid, a cell with a deleted TObject gives an access violation.
« Reply #3 on: March 19, 2025, 11:35:54 am »
Hi Zvoni
That's just the forum blocking the word ']"]>Blocked', but shouldn't that be ']"]>Blocked'?!?

HAHAHA...  ;D Gotcha - said the forum to the stupid Benny  :D

the word is ofc.: 'ttt eee mmm OOO' and the 2.nd 'TTT eee mmm uuu', let's see if that flies...  8)
edit2: Apparently them chinese tradesmen are quite invasive...
Regards Benny
« Last Edit: March 19, 2025, 11:42:46 am by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

Zvoni

  • Hero Member
  • *****
  • Posts: 2961
Re: StringGrid, a cell with a deleted TObject gives an access violation.
« Reply #4 on: March 19, 2025, 11:48:06 am »
Hi Zvoni
That's just the forum blocking the word ']"]>Blocked', but shouldn't that be ']"]>Blocked'?!?

HAHAHA...  ;D Gotcha - said the forum to the stupid Benny  :D

the word is ofc.: 'ttt eee mmm OOO' and the 2.nd 'TTT eee mmm uuu', let's see if that flies...  8)
edit2: Apparently them chinese tradesmen are quite invasive...
Regards Benny

You gotta be shitting me.... seriously?!?!??!
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

cdbc

  • Hero Member
  • *****
  • Posts: 2067
    • http://www.cdbc.dk
Re: StringGrid, a cell with a deleted TObject gives an access violation.
« Reply #5 on: March 19, 2025, 11:54:13 am »
Hi Zvoni
Nooo mate, I shit you not... The block is quite efficient too =^
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

Zvoni

  • Hero Member
  • *****
  • Posts: 2961
Re: StringGrid, a cell with a deleted TObject gives an access violation.
« Reply #6 on: March 19, 2025, 12:25:03 pm »
Hi Zvoni
Nooo mate, I shit you not... The block is quite efficient too =^
Regards Benny
What's next?
AliBaba and EBay?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

cdbc

  • Hero Member
  • *****
  • Posts: 2067
    • http://www.cdbc.dk
Re: StringGrid, a cell with a deleted TObject gives an access violation.
« Reply #7 on: March 19, 2025, 12:52:11 pm »
Hi
Probably, or maybe even 'ChatGPT'  :D :D :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

Hansvb

  • Hero Member
  • *****
  • Posts: 773
Re: StringGrid, a cell with a deleted TObject gives an access violation.
« Reply #8 on: March 19, 2025, 02:09:15 pm »
I wanted to write: I've completely lost you all.
But in the preview i see what you mean. The forum changes the var name automatically. Verry strange.





cdbc

  • Hero Member
  • *****
  • Posts: 2067
    • http://www.cdbc.dk
Re: StringGrid, a cell with a deleted TObject gives an access violation.
« Reply #9 on: March 19, 2025, 02:31:05 pm »
Hi Hans
Yup, so now we have to call it "PtrI_t_e_m_Object", to not have the forum translate even our code-snippets...  ;D
Yeah... Yippee \o/  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

Hansvb

  • Hero Member
  • *****
  • Posts: 773
Re: StringGrid, a cell with a deleted TObject gives an access violation.
« Reply #10 on: March 22, 2025, 10:03:08 am »
Thanks, I had indeed forgotten to remove the pointer.

 

TinyPortal © 2005-2018