Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1612
  • Last Modified:

Access violation in module MSDART.DLL

I have a Delphi program accessing a MS/SQL database.
I initially used the BDE to access it and everything ran fine.
For technical reasons too long to explain here, I have decided to 'migrate' the program to ADO components.
And here I have some problems.
The migrations was of course very simple, moving to TADOconnection etc...
But at execution, I first got a problem that CoInitialize was not performed, so after searching, I found I had to insert a CoInitialize(nil) instruction before to invoke these AFO objects.
But now, just before to terminate, the program issues an access violation in module MSDART.DLL. (I get this when running if from the Delphi development environment)
What the program does is
1. connect to the database
2. read some data in a table via a stored procedure
3. write some of this data to the standard output (writeln(text))
4. terminate
The data is correctly written to the standard output (the console) but an addition info is written there : runtime error 216.
Please help !
0
LeTay
Asked:
LeTay
1 Solution
 
Ivanov_GCommented:
Where is your TADOConnection? On a form or DataModule? Long time ago I had the same problem, but when the ADO Connection was moved to data module - it was fine...
0
 
LeTayAuthor Commented:
Neither on a form nor on a datamodule
I just create it at run time
0
 
Wim ten BrinkCommented:
The problem you're having is that you don't free any interface variable that gets initialized in your code. I've had the same problem and solved it like this:

program EnumADSI;

uses WhateverYouNeedToUse;

procedure ProcessDatabase;
begin
  // Whatever
end;

begin
  if Succeeded(CoInitializeEx(nil, COINIT_MULTITHREADED or COINIT_DISABLE_OLE1DDE or COINIT_SPEED_OVER_MEMORY)) then begin
    try
      // Call some procedure here that will do all the work with your database.  
      ProcessDatabase;
    except on E: Exception do WriteLn(E.Message);
    end;
    CoUninitialize;
  end;
end.

Whatever you do, try avoiding the use of global interface variables since Delphi is a bit lazy trying to clean them.

And don't forget to call CoUninitialize...

The runtime error just occurs because Delphi is trying to clean up some interfaces but for whatever reason they got messed up during the finalization.
0
 
LeTayAuthor Commented:
The remark of Ivanov (moving to a tdatamodule) looks going in the same direction that alex remark : some clean-up is not ... clean.
But now look at this :
I wrote this simple console application (I put some .../... on unuseful lines for this purpose) where there is nothing special done at the end (no couninitialize) and ... it works fine !
I think I need to have an indepth look at the code (much longer) that has the exception...
Here is the working code :
program P1;

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ADODB, DB, ActiveX;
{$R *.res}
var
 DB1:TADOConnection;
function ADOConnectSQL(DB1:TADOConnection):boolean;
begin
 with DB1 do
  begin
   ConnectionString  := .../...
   ConnectionTimeout := 15;
   CommandTimeout    := 300;
   KeepConnection    := True;
   LoginPrompt       := False;
  end;
 try
  DB1.Open;
  Writeln('DB open successful');
 except
  Result := False;
  Writeln('DB open failed');
  Exit;
 end;
 Result := True;
end;
procedure ADOGetSQL;
var
 Temp:string;
 SP:TADOStoredProc;
begin
 SP := TADOStoredProc.Create(Application);
 with SP do
  begin
   SP.Connection := DB1;
   ProcedureName := .../...;
   Parameters.Clear;
  end;
 with SP do
  begin
   Parameters.CreateParameter(.../...);
   try
    Prepared := True;
    ExecProc;
    Temp := Parameters.ParamByName(.../...);
    Free;
   except
    Exit;
   end;
  end;
 Writeln('DB data is ' + Temp);
end;
begin
 CoInitialize(nil);
 DB1 := TADOConnection.Create(Application);
 if ADOConnectSQL(DB1) then ADOGetSQL;
 DB1.Free;
end.
0
 
moduloCommented:
PAQed with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now