Solved

Program crashing on closing form/terminating

Posted on 2008-10-02
28
464 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:moorhouselondon
  • 16
  • 8
  • 2
  • +1
28 Comments
 
LVL 14

Expert Comment

by:SteveBay
Comment Utility
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
 
LVL 21

Expert Comment

by:ziolko
Comment Utility
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
 
LVL 31

Author Comment

by:moorhouselondon
Comment Utility
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
 
LVL 31

Author Comment

by:moorhouselondon
Comment Utility
: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
 
LVL 14

Expert Comment

by:SteveBay
Comment Utility
>> 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
 
LVL 31

Author Comment

by:moorhouselondon
Comment Utility
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
 
LVL 31

Author Comment

by:moorhouselondon
Comment Utility
Sorry, I should have said "I have now removed all Indy components, as per your suggestion..."  Problem is still occurring.
0
 
LVL 21

Expert Comment

by:ziolko
Comment Utility
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
 
LVL 31

Author Comment

by:moorhouselondon
Comment Utility
If the code in #22628557 is spliced to the code in the Q itself, that is now the complete program!
0
 
LVL 21

Expert Comment

by:ziolko
Comment Utility
>>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
 
LVL 6

Expert Comment

by:JosephGlosz
Comment Utility
would you try one simple thing: put in a sleep(5000) right after the FreeLibrary call?
0
 
LVL 31

Author Comment

by:moorhouselondon
Comment Utility
I'm calling it a night.  I have meetings to attend tomorrow but will be back here early evening (GMT).

0
 
LVL 31

Author Comment

by:moorhouselondon
Comment Utility
Just seen your comment Joseph... will try that right now...
0
 
LVL 31

Author Comment

by:moorhouselondon
Comment Utility
...and ziolko's too...
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 31

Author Comment

by:moorhouselondon
Comment Utility
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
 
LVL 6

Expert Comment

by:JosephGlosz
Comment Utility
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
 
LVL 21

Expert Comment

by:ziolko
Comment Utility
can you post here complete .pas and .dfm of this form?

ziolko.
0
 
LVL 31

Author Comment

by:moorhouselondon
Comment Utility
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
 
LVL 31

Author Comment

by:moorhouselondon
Comment Utility
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
 
LVL 31

Author Comment

by:moorhouselondon
Comment Utility
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
 
LVL 21

Expert Comment

by:ziolko
Comment Utility
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
 
LVL 21

Expert Comment

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

ziolko.
0
 
LVL 31

Author Comment

by:moorhouselondon
Comment Utility
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
 
LVL 31

Author Comment

by:moorhouselondon
Comment Utility
It sounds like this is a DLL version problem if you (ziolko) ran the program cleanly.
0
 
LVL 21

Expert Comment

by:ziolko
Comment Utility
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
 
LVL 31

Author Comment

by:moorhouselondon
Comment Utility
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
 
LVL 21

Accepted Solution

by:
ziolko earned 500 total points
Comment Utility
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
 
LVL 31

Author Closing Comment

by:moorhouselondon
Comment Utility
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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now