Board index » delphi » DAO Access Violation using threaded TQuery

DAO Access Violation using threaded TQuery

I explain myself. Maybe someone could help me, I don't understand.

I use a runtime create TQuery in a thread to load many descriptions from 2
databases successively (SQL-Server and Access). Everything works well until
the session closes. Here, I get an access violation id DAO3032.DLL. So I
tried to debug and found out that my TQuery's still "alive" after its free,
as well as my TThread derived instance. It points to an area which is not
TQuery at all. Why ?
Did I miss something. Here's the mask of my code if someone would like to
understand.

In Main application Window OnShow event

sqlLoad := TQuery.Create(nil) ;
sqlLoad.SessionName := Session.SessionName ;
LoadThread := TLoadThread.Create(True) ;
LoadThread.Resume ;

In my Thread unit :

TLoadThread = class(TThread)
                                constructor Create(Suspended:Boolean) ;
override;
                                procedure Execute ; override ;
                            end ;

constructor TLoadThread.Create(Suspended:Boolean) ;
begin
    inherited Create(Suspended) ;
    FreeOnTerminate := True ;
end ;

procedure TLoadThread.Execute ;
begin
    with sqlLoad do
    begin
        DatabaseName := 'XXXXX' ;
        sql.add('select xxxx from YYYY') ;
        Open ;
        ...
        ...
        Close ;
    end ;
end ;

And in my Main application's form OnDestroy event ;

sqlLoad.Free ;

Thanks for any help about Threading and Data Accessing.
Best regards.
Nicolas

 

Re:DAO Access Violation using threaded TQuery


MS Access and the DAO are NOT thread safe. This is documented by
Microsoft. You should avoid using threads for DAO.

Woody

Quote
Nicolas Letullier wrote in message <6nqqfp$8s...@front3.grolier.fr>...
>I explain myself. Maybe someone could help me, I don't understand.

>I use a runtime create TQuery in a thread to load many descriptions
from 2
>databases successively (SQL-Server and Access). Everything works well
until
>the session closes. Here, I get an access violation id DAO3032.DLL.
So I
>tried to debug and found out that my TQuery's still "alive" after its
free,
>as well as my TThread derived instance. It points to an area which is
not
>TQuery at all. Why ?
>Did I miss something. Here's the mask of my code if someone would
like to
>understand.

>In Main application Window OnShow event

>sqlLoad := TQuery.Create(nil) ;
>sqlLoad.SessionName := Session.SessionName ;
>LoadThread := TLoadThread.Create(True) ;
>LoadThread.Resume ;

>In my Thread unit :

>TLoadThread = class(TThread)
>                                constructor Create(Suspended:Boolean)
;
>override;
>                                procedure Execute ; override ;
>                            end ;

>constructor TLoadThread.Create(Suspended:Boolean) ;
>begin
>    inherited Create(Suspended) ;
>    FreeOnTerminate := True ;
>end ;

>procedure TLoadThread.Execute ;
>begin
>    with sqlLoad do
>    begin
>        DatabaseName := 'XXXXX' ;
>        sql.add('select xxxx from YYYY') ;
>        Open ;
>        ...
>        ...
>        Close ;
>    end ;
>end ;

>And in my Main application's form OnDestroy event ;

>sqlLoad.Free ;

>Thanks for any help about Threading and Data Accessing.
>Best regards.
>Nicolas

Other Threads