Program crashing on closing form/terminating

Simplified this problem to the bare-bones, as follows:-

This program opens up an SQLConnection to a MySQL database, shows a form then does nothing.  When user closes the form, get error message as shown below.  Though I create the SQLConnection programmatically, I am putting other components on the form (SQLtable, Provider, CDS, DataSource).  


When run from IDE I get this message when closing the form:

Project ... faulted with message: 'system exception (code 0xc00000008) at 0x7c90e4df'.  Process stopped. ...

When run directly I get:

The instruction at 0x... reference memory at 0x....  The memory could not be read.

What am I doing wrong?
{some of this code was supplied by Geert Gruez in a Q I asked a while ago http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_23616528.html}
 
  private
    { Private declarations }
    fLibSysMySQLHandle: THandle;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;
 
 
constructor TForm2.Create(AOwner: TComponent);
var Err: DWord;
begin
  inherited Create(AOwner);
  fLibSysMySQLHandle := LoadLibrary('libmysql.dll');
  Err := GetLastError;
  if (fLibSysMySQLHandle = 0) and (Err > 0) then
    ShowMessage('Error ' + IntToStr(Err));
end;
 
destructor TForm2.Destroy;
begin
  if fLibSysMySQLHandle > 0 then
    FreeLibrary(fLibSysMySQLHandle);
  inherited Destroy;
end;
 
 
 
 
procedure TForm2.FormShow(Sender: TObject);
var
  username,password:string;
  sqlconnection:tsqlconnection;
begin
username:='user';
password:='userpass';
SQLConnection := TSQLConnection.Create(nil);
SQLConnection.DriverName := 'MySQL';
SQLConnection.LibraryName := 'dbexpmys30.dll';
SQLConnection.VendorLib := 'libmysql.dll';
SQLConnection.GetDriverFunc := 'getSQLDriverMYSQL';
SQLConnection.Params.Clear;
SQLConnection.Params.Add('DriverName=MySQL');
SQLConnection.Params.Add('HostName=bloggs.co.uk');
SQLConnection.Params.Add('Database=bloggs');
SQLConnection.Params.Add('User_Name='+username);
SQLConnection.Params.Add('Password='+password);
SQLConnection.Params.Add('BlobSize=1000');
SQLConnection.Params.Add('LocaleCode=0000');
SQLConnection.Params.Add('Compressed=False');
SQLConnection.Params.Add('Encrypted=False');
SQLConnection.Params.Values['FetchAll'] := 'True';
sqlconnection.loginprompt:=false;
SQLConnection.Open;
sqltable1.SQLConnection:=sqlconnection;
 
showmessage('hello world');
 
{I thought that maybe the problem was due to creating the SQLConnection, but not properly destroying it afterwards, so I put this lot in}
 
sqltable1.SQLConnection:=nil;
sqlconnection.Close;
sqlconnection.Free;
sqlconnection:=nil;
end;

Open in new window

LVL 31
moorhouselondonAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ziolkoConnect With a Mentor Commented:
you said that you've reinstalled your machine, and have new Delphi, is it different version than previous one?

anyway for version sake, I used dll downloaded from this location:
http://www.dll-files.com/dllindex/dll-files.shtml?libmysql

ziolko.
0
 
SteveBayCommented:
Sounds like a misbehaving component to me.  I would try selectively removing components one at time to see if you can isolate the offender.
0
 
ziolkoCommented:
there's nothing wrong with this code, is that all or it is a part of bigger project?

anyway, when program crashes when run from IDE stop it and take a look at call stack.

ziolko.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
moorhouselondonAuthor Commented:
It has been cut-down from a utility I am writing which has some Indy components on it (IdMessage, IdPop3 and IdAntifreeze).  I have knocked out all code to do with Indy, but the components are still on the form.

btw, this is in Delphi 2007.
0
 
moorhouselondonAuthor Commented:
:7c90e4df ntdll.KiRaiseUserExceptionDispatcher + 0x37
:7c90e4f4 ntdll.KiFastSystemCallRet
:00d4cbfe ; C:\WINDOWS\system32\libmySQL.dll
:7c90118a ntdll.LdrInitializeThunk + 0x24
:7c923aba ; ntdll.dll
:7c81ca96 ; C:\WINDOWS\system32\kernel32.dll
:7c81cb0e kernel32.ExitProcess + 0x14
:00405229 @Halt0 + $C5



This is the Call Stack after crash.  I have no idea how to interpret this!
0
 
SteveBayCommented:
>> I have knocked out all code to do with Indy, but the components are still on the form.
Just because there is no code assicated with components does not mean that they will not execute OnCreate and OnDestroy methods
0
 
moorhouselondonAuthor Commented:
I have removed all Indy components, and replaced the procedure call (line 60 of the program listing in my Q - replaced with showmessage).  This is what I have at the top of the program:-

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdMessage, IdAntiFreezeBase, IdAntiFreeze, IdBaseComponent,
  IdComponent, IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase,
  IdMessageClient, IdPOP3, ExtCtrls, ComCtrls, WideStrings, FMTBcd, DBClient,
  DB, SqlExpr, Provider,idattachmentfile,idattachmentmemory,idtext;

type
  TForm2 = class(TForm)
    DataSource1: TDataSource;
    DataSetProvider1: TDataSetProvider;
    SQLTable1: TSQLTable;
    ClientDataSet1: TClientDataSet;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    lvheaders: TListView;
    TabSheet3: TTabSheet;
    lvmessageparts: TListView;
    TabSheet4: TTabSheet;
    Memo1: TMemo;
    procedure FormShow(Sender: TObject);


Do you think I need to remove the unused "uses" entries?  As you can see, I am using pagecontrol, tabsheets and listviews - are these likely to harbour any problem?
0
 
moorhouselondonAuthor Commented:
Sorry, I should have said "I have now removed all Indy components, as per your suggestion..."  Problem is still occurring.
0
 
ziolkoCommented:
my bet is on this DLL maybe there's some thread running in background that is not terminated properly, try removing code that loads/calls/releases library and see if error is still there

ziolko.
0
 
moorhouselondonAuthor Commented:
If the code in #22628557 is spliced to the code in the Q itself, that is now the complete program!
0
 
ziolkoCommented:
>>Do you think I need to remove the unused "uses" entries?
yes.

also comment-out code loading DLL, call stack indicates that there's problem.

ziolko.
0
 
JosephGloszCommented:
would you try one simple thing: put in a sleep(5000) right after the FreeLibrary call?
0
 
moorhouselondonAuthor Commented:
I'm calling it a night.  I have meetings to attend tomorrow but will be back here early evening (GMT).

0
 
moorhouselondonAuthor Commented:
Just seen your comment Joseph... will try that right now...
0
 
moorhouselondonAuthor Commented:
...and ziolko's too...
0
 
moorhouselondonAuthor Commented:
Put the sleep in, it paused, then crashed
Took out the load library, still no go
Took out most things from uses, still crashing on exit.
0
 
JosephGloszCommented:
the reason for the sleep is to give the DLL time to unload properly. I have a DLL that uses RIO that needs exactly that because of "lazy" or delayed cleanup when it is unloading. Without that sleep, my main program has a crash similar to yours.
0
 
ziolkoCommented:
can you post here complete .pas and .dfm of this form?

ziolko.
0
 
moorhouselondonAuthor Commented:
Here's the pas (dfm will follow shortly)

unit Unit2;
 
interface
 
uses
  Windows, Messages, SysUtils, {Variants,} Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, {FMTBcd,} DBClient, DB, SqlExpr, Provider, ComCtrls, FMTBcd {FMTBcd{, IdMessage, IdAntiFreezeBase, IdAntiFreeze, IdBaseComponent,
  IdComponent, IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase,
  IdMessageClient, IdPOP3, ExtCtrls, ComCtrls, WideStrings, FMTBcd, DBClient,
  DB, SqlExpr, Provider,idattachmentfile,idattachmentmemory,idtext};
 
type
  TForm2 = class(TForm)
    DataSource1: TDataSource;
    DataSetProvider1: TDataSetProvider;
    SQLTable1: TSQLTable;
    ClientDataSet1: TClientDataSet;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    TabSheet3: TTabSheet;
    TabSheet4: TTabSheet;
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    fLibSysMySQLHandle: THandle;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;
 
var
  Form2: TForm2;
 
implementation
 
{$R *.dfm}
var
  fname,shortdesc,longdesc:string;
  intindex:integer;
 
 
 
 
 
constructor TForm2.Create(AOwner: TComponent);
var Err: DWord;
begin
  inherited Create(AOwner);
{  fLibSysMySQLHandle := LoadLibrary('libmysql.dll');}
  Err := GetLastError;
  if (fLibSysMySQLHandle = 0) and (Err > 0) then
    ShowMessage('Error ' + IntToStr(Err));
end;
 
destructor TForm2.Destroy;
begin
  if fLibSysMySQLHandle > 0 then
{    FreeLibrary(fLibSysMySQLHandle);}
  inherited Destroy;
end;
 
 
 
procedure TForm2.FormShow(Sender: TObject);
var
  nextid,nextmessage:integer;
  username,password:string;
  sqlconnection:tsqlconnection;
begin
nextid:=0;
username:='user';
password:='userpass';
SQLConnection := TSQLConnection.Create(nil);
SQLConnection.DriverName := 'MySQL';
SQLConnection.LibraryName := 'dbexpmys30.dll';
SQLConnection.VendorLib := 'libmysql.dll';
SQLConnection.GetDriverFunc := 'getSQLDriverMYSQL';
SQLConnection.Params.Clear;
SQLConnection.Params.Add('DriverName=MySQL');
SQLConnection.Params.Add('HostName=bloggs.co.uk');
SQLConnection.Params.Add('Database=bloggs');
SQLConnection.Params.Add('User_Name='+username);
SQLConnection.Params.Add('Password='+password);
SQLConnection.Params.Add('BlobSize=1000');
SQLConnection.Params.Add('LocaleCode=0000');
SQLConnection.Params.Add('Compressed=False');
SQLConnection.Params.Add('Encrypted=False');
SQLConnection.Params.Values['FetchAll'] := 'True';
sqlconnection.loginprompt:=false;
SQLConnection.Open;
sqltable1.SQLConnection:=sqlconnection;
sqltable1.SQLConnection:=nil;
sqlconnection.Close;
sqlconnection.Free;
sqlconnection:=nil;
end;
 
end.

Open in new window

0
 
moorhouselondonAuthor Commented:
Here's dfm

btw in case you were wondering, there is no Tform1.

object Form2: TForm2
  Left = 0
  Top = 0
  Caption = 'Form2'
  ClientHeight = 387
  ClientWidth = 508
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnShow = FormShow
  PixelsPerInch = 96
  TextHeight = 13
  object PageControl1: TPageControl
    Left = 0
    Top = 0
    Width = 508
    Height = 387
    ActivePage = TabSheet4
    Align = alClient
    TabOrder = 0
    object TabSheet1: TTabSheet
      Caption = 'POP Account Detail'
    end
    object TabSheet2: TTabSheet
      Caption = 'Message Headers'
      ImageIndex = 1
    end
    object TabSheet3: TTabSheet
      Caption = 'Attachment List'
      ImageIndex = 2
    end
    object TabSheet4: TTabSheet
      Caption = 'Long Description'
      ImageIndex = 3
    end
  end
  object DataSource1: TDataSource
    Left = 256
    Top = 256
  end
  object DataSetProvider1: TDataSetProvider
    Left = 176
    Top = 256
  end
  object SQLTable1: TSQLTable
    Left = 112
    Top = 256
  end
  object ClientDataSet1: TClientDataSet
    Aggregates = <>
    Params = <>
    Left = 216
    Top = 256
  end
end

Open in new window

0
 
moorhouselondonAuthor Commented:
If this is problem is due to a subtle difference in Library versions, how can I ensure I have the correct versions?  Searching on the internet there seems to be some controversy over what works and what doesn't.
0
 
ziolkoCommented:
as I expected, no problem for me.
this form is just shown and nothing more?
do you have any other references to this dll?

ziolko.
0
 
ziolkoCommented:
..of course I don't have database so I skipped SQLConnection.Open; but everything else works, even loading DLL.

ziolko.
0
 
moorhouselondonAuthor Commented:
I have nothing else using this dll.

A bit of history:-

I had some programs using MySQL successfully.  Hard drive caved in.  Installed new hard drive.  Installed Delphi from scratch (no mean feat, I bought it as a download from Codegear's site).  Had problems modifying existing MySQL programs, runtime errors.  Fiddled around with library statements to get to the assignments shown above, plus the problem which Geert Gruez solved (see my previous Q).  This works but I get this crash when terminating the program.
0
 
moorhouselondonAuthor Commented:
It sounds like this is a DLL version problem if you (ziolko) ran the program cleanly.
0
 
ziolkoCommented:
huh strange, there's always brutal method - remove this form from project create new empty one and start adding components one-by-one starting with standard ones.

I'm heading to bed now.. maybe something will popup in my head untill tomorrow morning:)

ziolko.
0
 
moorhouselondonAuthor Commented:
Off to bed now... feeling verrrry slleeeeepy zzzzz

Will be back tomorrow.  I think progress has been made.... Thanks for everyone's input so far....
0
 
moorhouselondonAuthor Commented:
Ziolko's hit the jackpot!  

22.10.06 1,519,616 bytes : file downloaded from ziolko's link
25.10.06 2,031,616 bytes : file I was using

Thanks to all who contributed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.