[SOLVED] Trying to move SQLdb components into a DataModule and getting SIGSEGV lazarus / 16-Databases ---------------------------------------------------------------------- mdlueck · 2011-08-12 15:44 I wanted to move my DB components into a DataModule to share them across multiple forms. I did so, code finally compiles fine (After moving the DataModule up in the project's .lpi file and adding the DM's unit in the Form's uses list), however I get a SIGSEGV when I try to run it. On the Form I have the following code: Code: [Select]procedure TForm1.FormCreate(Sender: TObject); var   ARegistry: TRegistry;   strDatabaseName: String; begin   //Find the correct DatabaseName from the registry   ARegistry := TRegistry.Create;   ARegistry.RootKey := HKEY_LOCAL_MACHINE;   ARegistry.OpenKeyReadOnly('\Software\CompanyNameHere\Fandango');   strDatabaseName := ARegistry.ReadString('DatabaseName');   ARegistry.Free;   //Set the DatabaseName found in the registry   [color=red]DataModule1.ODBCConnection1.DatabaseName := strDatabaseName;[/color]   //Enable all of the various components, order matters   DataModule1.ODBCConnection1.Connected := True;   DataModule1.SQLTransaction1.Active := True;   DataModule1.SQLQuery1.Active := True;   DBGrid1.Enabled := True; end;  It blows up on the line indicated in red. Code works beautifully in the version where I placed the DB objects on the form itself. Suggestions please? ---------------------------------------------------------------------- Dick, from the internet · 2011-08-12 17:32 Did you actually test and verify that strDataBaseName actually holds the correct value Code: [Select]showmessage(strDataBaseName); Or how about using Code: [Select] DataModule1.ODBCConnection1.DatabaseName :=  '''' + strDatabaseName + '''';  In the event there is a space or odd character in the database name.   geno. ---------------------------------------------------------------------- mdlueck · 2011-08-12 17:47 Quote from: geno on August 12, 2011, 07:32:06 pmOr how about using Code: [Select] DataModule1.ODBCConnection1.DatabaseName :=  '''' + strDatabaseName + ''''; Still blows up. It works absolutely fine if I comment out the dynamic setting of the DatabaseName and the activation of the various SQLdb components... and of course manually set the DBName and activate the SQLdb components. It was working equally well when the four SQLdb components were on the form itself. But moving them to DataModule1 and specifying the longer name for those various components appears to have made something mad. Thus I know I do not have a typo in the registry. Just trying some baby steps to get comfortable with Laz, and encountering some challenges. Planning to add data validation code in further baby steps. ---------------------------------------------------------------------- bertherngreen · 2011-08-12 18:39 I can't see the rest of your code, but do you first create an instance of the datamodule? e.g.: Code: [Select]procedure TForm1.FormCreate(Sender: TObject); begin   DataModule1 := TDataModule1.Create(Self); ... or if you create the instance in the lpr make sure that you have the datamodule created before the forms' instance: Code: [Select]  Application.Initialize;   Application.CreateForm(TDataModule1, DataModule1);   Application.CreateForm(TForm1, Form1); ---------------------------------------------------------------------- mdlueck · 2011-08-12 19:25 Quote from: bertherngreen on August 12, 2011, 08:39:48 pmI can't see the rest of your code, but do you first create an instance of the datamodule? I first started to comment on this... that I can make it work if I comment out the setting of the DB name and activation of the DB components... thus I did not think I must create an instance of the DataModule. However... this is where you uncovered the root cause!!! Quote from: bertherngreen on August 12, 2011, 08:39:48 pmor if you create the instance in the lpr make sure that you have the datamodule created before the forms' instance: Code: [Select]  Application.Initialize;   Application.CreateForm(TDataModule1, DataModule1);   Application.CreateForm(TForm1, Form1); I had never opened the .lpr file... was thinking it was the .lpi file I was suppose to correctly order the various units in. This makes much more sense to hand edit the .lpr file! Indeed since I created the DM after the form, it was beneath the form. Swapped order / program now works. Thus mega kudos to you, bertherngreen!!! (I was about to toss Laz out the nearest window and give up on it... for the umpteenth time!) Thank you!