Forum > General
More objects and object list questions[SOLVED]
jbmckim:
This is a companion to my previous question on oject lists. This one however attempts to use a different approach to reading a list of objects. It has a problem that I can't figure out.
This demo program is built from two units. (Most of the main unit is display.)
Here's the main unit:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit MainFormUnit; {$mode objfpc}{$H+} interface uses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, InsUnit, fgl; type TInsList = specialize TFPGObjectList<TIns>; type { TForm1 } TForm1 = class(TForm) ShowListButton: TButton; ShowObjectsButton: TButton; Memo1: TMemo; procedure FormCreate(Sender: TObject); procedure ShowListButtonClick(Sender: TObject); procedure ShowObjectsButtonClick(Sender: TObject); private public end; const I_NUM_ENTRIES : integer = 4; var Form1: TForm1; InsList : TInsList; implementation {$R *.lfm} { TForm1 } procedure TForm1.ShowObjectsButtonClick(Sender: TObject);var i : integer = 0; InsObj : TIns; begin InsList := TInsList.Create; for i := 0 to I_NUM_ENTRIES do begin InsObj := TIns.Create; InsObj.Init(); InsObj.RunTimeInsNumber := i; InsObj.InsSerial := 'testSerialNum' + IntToStr(i); InsObj.InsName := 'Name ' + IntToStr(i); InsList.add(InsObj); Memo1.Lines.Add(IntToStr(InsObj.RunTimeInsNumber)); Memo1.Lines.Add(InsObj.InsSerial); Memo1.Lines.Add(InsObj.InsName); Memo1.Lines.Add(' '); InsObj.Free; end; ShowListButton.Enabled:=true; end; procedure TForm1.ShowListButtonClick(Sender: TObject);var i : integer = 0; InsObj : TIns; var p : pointer; begin i := InsList.Count; Memo1.Lines.Add('--------------- '); Memo1.Lines.Add('Items from list '); Memo1.Lines.Add('--------------- '); Memo1.Lines.Add(' '); for i := 0 to I_NUM_ENTRIES do begin InsObj := TIns.Create; InsObj.Init(); InsObj.RunTimeInsNumber:= InsList[i].RunTimeInsNumber; InsObj.InsSerial := InsList[i].InsSerial; InsObj.InsName:=InsList[i].InsName; Memo1.Lines.Add(IntToStr(InsObj.RunTimeInsNumber)); Memo1.Lines.Add(InsObj.InsSerial); Memo1.Lines.Add(InsObj.InsName); end; end; procedure TForm1.FormCreate(Sender: TObject);begin ShowListButton.Enabled:=false;end; end.
Here's the unit that contains the class from which I want to build a list:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit InsUnit; {$mode objfpc}{$H+} interface uses Classes, SysUtils; typeTIns = Class public RunTimeInsNumber : integer; SortOrderInsNumber : integer; InsName : string; InsSerial : string; DBFeatureStr : string; constructor Init(); private const defaultSpecLoad = 'Testing Private'; public const SpecDefaults = 'test'; end; implementation constructor TIns.Init();begin RuntimeInsNumber := 0; SortOrderInsNumber :=0; InsName :=''; InsSerial :=''; DBFeatureStr :=''; end; end.
The program dies on the line InsObj.InsSerial := InsList.InsSerial; in the section that attempts to display the values after they've been loaded to the list. I'm guessing this is a pointer/value problem but I don't get why or how to get this demo to work.
If it would help, I can upload the entire project. Let me know how to so/what you'd prefer if you're interested.
EDIT: Ooops, this throws the SIGSEGV error with stack trace.
Thanks in advance.
jamie:
as with many objects that have index functions they are 0 based...
What this means is if you have X-NUMBEROF_ENTRIies, you start at 0 and end at COUNT-1
So if a list has 10 items and you want to index them...
For I := 0 To 9 do.....
or
For I := 0 To SomeList.Count-1 do...
0 index counts as the first item..
molly:
Offending line highlighted below.
--- Quote from: jbmckim on February 23, 2018, 10:46:27 pm ---
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit MainFormUnit; {$mode objfpc}{$H+} interface uses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, InsUnit, fgl; type TInsList = specialize TFPGObjectList<TIns>; type { TForm1 } TForm1 = class(TForm) ShowListButton: TButton; ShowObjectsButton: TButton; Memo1: TMemo; procedure FormCreate(Sender: TObject); procedure ShowListButtonClick(Sender: TObject); procedure ShowObjectsButtonClick(Sender: TObject); private public end; const I_NUM_ENTRIES : integer = 4; var Form1: TForm1; InsList : TInsList; implementation {$R *.lfm} { TForm1 } procedure TForm1.ShowObjectsButtonClick(Sender: TObject);var i : integer = 0; InsObj : TIns; begin InsList := TInsList.Create; for i := 0 to I_NUM_ENTRIES do begin InsObj := TIns.Create; InsObj.Init(); InsObj.RunTimeInsNumber := i; InsObj.InsSerial := 'testSerialNum' + IntToStr(i); InsObj.InsName := 'Name ' + IntToStr(i); InsList.add(InsObj); Memo1.Lines.Add(IntToStr(InsObj.RunTimeInsNumber)); Memo1.Lines.Add(InsObj.InsSerial); Memo1.Lines.Add(InsObj.InsName); Memo1.Lines.Add(' '); InsObj.Free; end; ShowListButton.Enabled:=true; end; procedure TForm1.ShowListButtonClick(Sender: TObject);var i : integer = 0; InsObj : TIns; var p : pointer; begin i := InsList.Count; Memo1.Lines.Add('--------------- '); Memo1.Lines.Add('Items from list '); Memo1.Lines.Add('--------------- '); Memo1.Lines.Add(' '); for i := 0 to I_NUM_ENTRIES do begin InsObj := TIns.Create; InsObj.Init(); InsObj.RunTimeInsNumber:= InsList[i].RunTimeInsNumber; InsObj.InsSerial := InsList[i].InsSerial; InsObj.InsName:=InsList[i].InsName; Memo1.Lines.Add(IntToStr(InsObj.RunTimeInsNumber)); Memo1.Lines.Add(InsObj.InsSerial); Memo1.Lines.Add(InsObj.InsName); end; end; procedure TForm1.FormCreate(Sender: TObject);begin ShowListButton.Enabled:=false;end; end.
--- End quote ---
molly:
--- Quote from: jamie on February 23, 2018, 11:10:20 pm ---as with many objects that have index functions they are 0 based...
What this means is if you have X-NUMBEROF_ENTRIies, you start at 0 and end at COUNT-1
--- End quote ---
Although i would recommend that as well, the OP is using a const (and as such is valid, assuming that all objects were created correctly and added to the list).
FWIW: OP has more issues in the code that should be addressed.
This is how it could look when using FPC:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program example; {$MODE OBJFPC}{$H+} uses Classes, SysUtils, fgl; type TIns = class private const defaultSpecLoad = 'Testing Private'; public const SpecDefaults = 'test'; public RunTimeInsNumber : integer; SortOrderInsNumber : integer; InsName : string; InsSerial : string; DBFeatureStr : string; public constructor Create; end; constructor TIns.Create;begin inherited; RuntimeInsNumber := 0; SortOrderInsNumber :=0; InsName :=''; InsSerial :=''; DBFeatureStr :='';end; type TInsList = specialize TFPGObjectList<TIns>; const I_NUM_ENTRIES = 4; var InsList : TInsList; procedure AddObjects;var i: integer; InsObj : TIns;begin for i := 0 to I_NUM_ENTRIES do begin InsObj := TIns.Create; InsObj.RunTimeInsNumber := i; InsObj.InsSerial := 'testSerialNum' + IntToStr(i); InsObj.InsName := 'Name ' + IntToStr(i); InsList.Add(InsObj); WriteLn(IntToStr(InsObj.RunTimeInsNumber)); WriteLn(InsObj.InsSerial); WriteLn(InsObj.InsName); WriteLn(' '); end;end; procedure ShowList;var InsObj : TIns;begin WriteLn('--------------- '); WriteLn('Items from list '); WriteLn('--------------- '); WriteLn(' '); for insObj in Inslist do begin WriteLn(IntToStr(InsObj.RunTimeInsNumber)); WriteLn(InsObj.InsSerial); WriteLn(InsObj.InsName); end;end; procedure Creation;begin InsList := TInsList.Create;end; procedure Destruction;begin InsList.Free;end; begin Creation; AddObjects; ShowList; Destruction;end.
jbmckim:
molly,
Yes. That's it.
Any help understanding why freeing the object in this context trashes the list...or whatever the reason might be...would be appreciated.
Navigation
[0] Message Index
[#] Next page