I apologise for emailing but need a little bit of help please.
Creating form components dynamically is a new area of pascal programming for me and I have become stuck. My application is supposed to be an editor that can open multiple files for editing. I have a page control and when opening a new file I add a page and place a synedit on the page on it's own, into which the file is loaded. The loading works fine - I create a stringlist, loadfromfile, then SetObjectProp(Component, PropInfo['Lines'], StringList). StringList.Free works without error.
However, when I try to save the file the following code works, insomuch the file is saved to disk, but the application crashes when trying to free the stringlist (HTMLFile.Free) with the error 'Project raised exception class 'External: SIGSEGV' At address 4201C3'. If I don't free the stringlist, the application does not crash but I'm left with a memory leak as indicated in my heap.trc file.
I have a feeling that HTMLFile is not actually a stringlist, I'm just forcing it to be one here - HTMLFile := TStringList(GetObjectProp(Comp, PropInfo, TStrings)); If that's the case, how can I get and/or save the text from the dynamically created synedit. This has been puzzling me for days now!!!!
Many thanks for your help.
Martin Collins
Using Lazarus 1.6, FPC 3.0.0, Linux x86_64 gtk2
####
procedure TMainForm.SaveSynEdit(ThisPage: Integer);
var
HTMLFile: TStringList;
Filename: String;
Comp : TComponent;
PropInfo: PPropInfo;
begin
FileSaveDialog.Title:= 'Save File...';
FileSaveDialog.DefaultExt := 'html'; //default extension
FileSaveDialog.Filter := 'HTML source file|*.html';
Filename:= FileList[ThisPage];
FileSaveDialog.FileName:= Filename + '.html';
If FileSaveDialog.Execute then
begin
HTMLFile:= TStringList.Create;
Comp := PageControlEditor.ActivePage.Controls[0];
If Comp is TWinControl then
begin
If IsPublishedProp(Comp, 'Lines') then
begin
PropInfo := GetPropInfo(Comp, 'Lines', [tkClass]);
HTMLFile := TStringList(GetObjectProp(Comp, PropInfo, TStrings));
Filename:= FileSaveDialog.FileName;
HTMLFile.SaveToFile(Filename);
HTMLFile.Free;
FileList[ThisPage]:= Filename;
PageControlEditor.ActivePage.Caption:= ExtractFileName(Filename);
PageControlEditor.ActivePage.Font.Style:= [];
PageControlEditor.ActivePage.Font.Color:= clDefault;
end;
end;
end;
end;