C++, ADO, Threads give Access Violation? 
Author Message
 C++, ADO, Threads give Access Violation?

I'm stuck. I have created a thread that needs to insert a record into a SQL
table. I'm using ADO and create the connection object as follows:

_ConnectionPtr m_pConn = NULL;

m_pConn.CreateInstance(__uuidof(Connection));

if (m_pConn)

{

    m_pConn->CursorLocation = adUseClient;

    m_pConn->Open(L"(connection stuff)", "", "", adConnectUnspecified);

Quote:
}

Later in the thread I have the following:

if (m_pConn)

{

    try

    {

        _CommandPtr pUpdCmd(__uuidof(Command));

        pUpdCmd->ActiveConnection = m_pConn;

        char szCmd[100];

        sprintf(szCmd, "INSERT INTO ....(stuff omitted here).........);

        pUpdCmd->CommandText = szCmd;

        pUpdCmd->CommandType = adCmdText;

    }

    catch(_com_error& e)

    {

        _bstr_t bstrError = e.Description();

        m_pConn = NULL;

    }

Quote:
}

I get "unhandled exception in xxxx.exe: 0xC0000005: Access Violation." when
the pUpdCmd->CommandText = szCmd; is executed. An exception is NOT caught.

Any help would be greatly appreciated.

Thank you.



Sat, 06 Aug 2005 03:11:28 GMT  
 C++, ADO, Threads give Access Violation?
Every thread that uses OLE must initialize OLE.

Try putting  ::CoInitialize(NULL) at the start of your thread procedure and
::CoUninitialize() at the end of your thread procedure.

TFM3

Note: Spam-resistant e-mail address


Quote:
> I'm stuck. I have created a thread that needs to insert a record into a
SQL
> table. I'm using ADO and create the connection object as follows:

> _ConnectionPtr m_pConn = NULL;

> m_pConn.CreateInstance(__uuidof(Connection));

> if (m_pConn)

> {

>     m_pConn->CursorLocation = adUseClient;

>     m_pConn->Open(L"(connection stuff)", "", "", adConnectUnspecified);

> }

> Later in the thread I have the following:

> if (m_pConn)

> {

>     try

>     {

>         _CommandPtr pUpdCmd(__uuidof(Command));

>         pUpdCmd->ActiveConnection = m_pConn;

>         char szCmd[100];

>         sprintf(szCmd, "INSERT INTO ....(stuff omitted here).........);

>         pUpdCmd->CommandText = szCmd;

>         pUpdCmd->CommandType = adCmdText;

>     }

>     catch(_com_error& e)

>     {

>         _bstr_t bstrError = e.Description();

>         m_pConn = NULL;

>     }

> }

> I get "unhandled exception in xxxx.exe: 0xC0000005: Access Violation."
when
> the pUpdCmd->CommandText = szCmd; is executed. An exception is NOT caught.

> Any help would be greatly appreciated.

> Thank you.



Sat, 06 Aug 2005 03:20:28 GMT  
 C++, ADO, Threads give Access Violation?
Rick,

can you debug into the pUpdCmd->CommandText = szCmd statement.

that set of code should first convert the char * string to a _bstr_t, then
fetch the interface pointer to the underlying Command object, then execute
the put_CommandText method though the interface pointer (i.e. the raw_xxx
methods).

If COM had not been initialized, i strongly suspect the code would have
generated an access violation much earlier-- especially at the
pUpdCmd->ActiveConnection = m_pConn statement.

regards
roy fine


Quote:
> I'm stuck. I have created a thread that needs to insert a record into a
SQL
> table. I'm using ADO and create the connection object as follows:

> _ConnectionPtr m_pConn = NULL;

> m_pConn.CreateInstance(__uuidof(Connection));

> if (m_pConn) {
>     m_pConn->CursorLocation = adUseClient;
>     m_pConn->Open(L"(connection stuff)", "", "", adConnectUnspecified);
> }
> Later in the thread I have the following:

> if (m_pConn){
>     try  {
>         _CommandPtr pUpdCmd(__uuidof(Command));
>         pUpdCmd->ActiveConnection = m_pConn;
>         char szCmd[100];
>         sprintf(szCmd, "INSERT INTO ....(stuff omitted here).........);
>         pUpdCmd->CommandText = szCmd;
>         pUpdCmd->CommandType = adCmdText;
>     }
>     catch(_com_error& e)   {
>         _bstr_t bstrError = e.Description();
>         m_pConn = NULL;
>     }
> }

> I get "unhandled exception in xxxx.exe: 0xC0000005: Access Violation."
when
> the pUpdCmd->CommandText = szCmd; is executed. An exception is NOT caught.

> Any help would be greatly appreciated.

> Thank you.



Sat, 06 Aug 2005 03:41:15 GMT  
 C++, ADO, Threads give Access Violation?

Quote:
> that set of code should first convert the char * string to a _bstr_t, then
> fetch the interface pointer to the underlying Command object, then execute
> the put_CommandText method though the interface pointer (i.e. the raw_xxx
> methods).

True, that would have been more explicit as to what is actually happening,
however, since it compiles, it must be implictly casting.

Quote:

> If COM had not been initialized, i strongly suspect the code would have
> generated an access violation much earlier-- especially at the
> pUpdCmd->ActiveConnection = m_pConn statement.

Also true, which is why I suspect the SQL statement being formated in
sprintf is overrunning the char[100] array, overwriting the pointer stored
in pUpdCmd, hence causing access violations when the pointer is
dereferenced.


Sat, 06 Aug 2005 04:50:08 GMT  
 C++, ADO, Threads give Access Violation?
I do have the CoInitialize(NULL) in my InitInstance().


Quote:
> Every thread that uses OLE must initialize OLE.

> Try putting  ::CoInitialize(NULL) at the start of your thread procedure
and
> ::CoUninitialize() at the end of your thread procedure.

> TFM3

> Note: Spam-resistant e-mail address



> > I'm stuck. I have created a thread that needs to insert a record into a
> SQL
> > table. I'm using ADO and create the connection object as follows:

> > _ConnectionPtr m_pConn = NULL;

> > m_pConn.CreateInstance(__uuidof(Connection));

> > if (m_pConn)

> > {

> >     m_pConn->CursorLocation = adUseClient;

> >     m_pConn->Open(L"(connection stuff)", "", "", adConnectUnspecified);

> > }

> > Later in the thread I have the following:

> > if (m_pConn)

> > {

> >     try

> >     {

> >         _CommandPtr pUpdCmd(__uuidof(Command));

> >         pUpdCmd->ActiveConnection = m_pConn;

> >         char szCmd[100];

> >         sprintf(szCmd, "INSERT INTO ....(stuff omitted here).........);

> >         pUpdCmd->CommandText = szCmd;

> >         pUpdCmd->CommandType = adCmdText;

> >     }

> >     catch(_com_error& e)

> >     {

> >         _bstr_t bstrError = e.Description();

> >         m_pConn = NULL;

> >     }

> > }

> > I get "unhandled exception in xxxx.exe: 0xC0000005: Access Violation."
> when
> > the pUpdCmd->CommandText = szCmd; is executed. An exception is NOT
caught.

> > Any help would be greatly appreciated.

> > Thank you.



Sat, 06 Aug 2005 04:49:44 GMT  
 C++, ADO, Threads give Access Violation?
Rick,

::CoInitialize MUST be called in EVERY thread, not once per application run.

roy fine


Quote:
> I do have the CoInitialize(NULL) in my InitInstance().



> > Every thread that uses OLE must initialize OLE.

> > Try putting  ::CoInitialize(NULL) at the start of your thread procedure
> and
> > ::CoUninitialize() at the end of your thread procedure.

> > TFM3

> > Note: Spam-resistant e-mail address



> > > I'm stuck. I have created a thread that needs to insert a record into
a
> > SQL
> > > table. I'm using ADO and create the connection object as follows:

> > > _ConnectionPtr m_pConn = NULL;

> > > m_pConn.CreateInstance(__uuidof(Connection));

> > > if (m_pConn)

> > > {

> > >     m_pConn->CursorLocation = adUseClient;

> > >     m_pConn->Open(L"(connection stuff)", "", "",

adConnectUnspecified);

- Show quoted text -

Quote:

> > > }

> > > Later in the thread I have the following:

> > > if (m_pConn)

> > > {

> > >     try

> > >     {

> > >         _CommandPtr pUpdCmd(__uuidof(Command));

> > >         pUpdCmd->ActiveConnection = m_pConn;

> > >         char szCmd[100];

> > >         sprintf(szCmd, "INSERT INTO ....(stuff omitted
here).........);

> > >         pUpdCmd->CommandText = szCmd;

> > >         pUpdCmd->CommandType = adCmdText;

> > >     }

> > >     catch(_com_error& e)

> > >     {

> > >         _bstr_t bstrError = e.Description();

> > >         m_pConn = NULL;

> > >     }

> > > }

> > > I get "unhandled exception in xxxx.exe: 0xC0000005: Access Violation."
> > when
> > > the pUpdCmd->CommandText = szCmd; is executed. An exception is NOT
> caught.

> > > Any help would be greatly appreciated.

> > > Thank you.



Sat, 06 Aug 2005 14:18:50 GMT  
 C++, ADO, Threads give Access Violation?
Quote:
> ::CoInitialize MUST be called in EVERY thread [that uses COM], not once

per application run.


Sat, 06 Aug 2005 19:50:55 GMT  
 C++, ADO, Threads give Access Violation?
I understand. I have placed it immediately prior to the _CommandPtr
definition to make sure it was called in this thread too. Still no
difference.


Quote:
> Rick,

> ::CoInitialize MUST be called in EVERY thread, not once per application
run.

> roy fine



> > I do have the CoInitialize(NULL) in my InitInstance().



> > > Every thread that uses OLE must initialize OLE.

> > > Try putting  ::CoInitialize(NULL) at the start of your thread
procedure
> > and
> > > ::CoUninitialize() at the end of your thread procedure.

> > > TFM3

> > > Note: Spam-resistant e-mail address



> > > > I'm stuck. I have created a thread that needs to insert a record
into
> a
> > > SQL
> > > > table. I'm using ADO and create the connection object as follows:

> > > > _ConnectionPtr m_pConn = NULL;

> > > > m_pConn.CreateInstance(__uuidof(Connection));

> > > > if (m_pConn)

> > > > {

> > > >     m_pConn->CursorLocation = adUseClient;

> > > >     m_pConn->Open(L"(connection stuff)", "", "",
> adConnectUnspecified);

> > > > }

> > > > Later in the thread I have the following:

> > > > if (m_pConn)

> > > > {

> > > >     try

> > > >     {

> > > >         _CommandPtr pUpdCmd(__uuidof(Command));

> > > >         pUpdCmd->ActiveConnection = m_pConn;

> > > >         char szCmd[100];

> > > >         sprintf(szCmd, "INSERT INTO ....(stuff omitted
> here).........);

> > > >         pUpdCmd->CommandText = szCmd;

> > > >         pUpdCmd->CommandType = adCmdText;

> > > >     }

> > > >     catch(_com_error& e)

> > > >     {

> > > >         _bstr_t bstrError = e.Description();

> > > >         m_pConn = NULL;

> > > >     }

> > > > }

> > > > I get "unhandled exception in xxxx.exe: 0xC0000005: Access
Violation."
> > > when
> > > > the pUpdCmd->CommandText = szCmd; is executed. An exception is NOT
> > caught.

> > > > Any help would be greatly appreciated.

> > > > Thank you.



Sun, 07 Aug 2005 04:48:38 GMT  
 C++, ADO, Threads give Access Violation?
Your suspicion was correct. It was stupid error on my part and I didn't
check the size of the command buffer. I increased the size of the buffer and
all is well.

Thank you very much.


Quote:
> > that set of code should first convert the char * string to a _bstr_t,
then
> > fetch the interface pointer to the underlying Command object, then
execute
> > the put_CommandText method though the interface pointer (i.e. the
raw_xxx
> > methods).

> True, that would have been more explicit as to what is actually happening,
> however, since it compiles, it must be implictly casting.

> > If COM had not been initialized, i strongly suspect the code would have
> > generated an access violation much earlier-- especially at the
> > pUpdCmd->ActiveConnection = m_pConn statement.

> Also true, which is why I suspect the SQL statement being formated in
> sprintf is overrunning the char[100] array, overwriting the pointer stored
> in pUpdCmd, hence causing access violations when the pointer is
> dereferenced.



Sun, 07 Aug 2005 04:52:59 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Help: Return from function giving access violation!

2. malloc gives access violation exception

3. Please help: why gives me an Access Violation

4. Please help: why gives me an Access Violation

5. Help: Return from function giving access violation!

6. Splitter Windows Make FrameWnd::OnClose give Access Violation

7. Registry calls give ACCESS VIOLATION

8. ShellExecute gives access violation?

9. Please help: why gives me an Access Violation

10. Open Connection Access Violation for ADO in ATL COM

11. Help Please! ADO Access Violation in VC++

12. Access Violation when ADO is used from global object exported from MFC extensions DLL (VC++)

 

 
Powered by phpBB® Forum Software