Re: ADODB Command memory leak




Thanks for the sample. I ran it and indeed it does not leak memory.

I'm not sure how I can have a pointer leak if simply commenting out the
single Command Execute line stops the leak?? Any ideas?

Thanks,
Ken

"Matt Neerincx [MS]" wrote:

> I suspect you have a pointer leak in your code. It is quite easy to do in
> C++, as we all know. (G)
>
> Following code I just wrote works for me, no leaks ->
>
> // START SAMPLE
> // JetParam.cpp : Defines the entry point for the console application.
> //
>
> #include <windows.h>
> #include <conio.h>
> #include <stdio.h>
>
> #undef EOF
> #import <msado15.dll> no_namespace // Add full path to msado15.dll to vc++
> include path.
>
> _bstr_t JET_CONNECT(L"Provider=Microsoft.Jet.OLEDB.4.0;Data
> Source=C:\\NW99.mdb;");
> #define LOOP_COUNT 100000 // Number of loops to execute.
>
> _ConnectionPtr GetJetConnection()
> {
> HRESULT hr;
> _ConnectionPtr conn = NULL;
>
> hr = conn.CreateInstance( __uuidof(Connection) );
> //if ( FAILED( hr ) ) throw( _com_error( hr, NULL ) );
>
> conn->CursorLocation = adUseServer;
>
> // Open connection.
> conn->Open( JET_CONNECT, "", "", -1L );
>
> return conn;
>
> }
>
> void main(void)
> {
> HRESULT hr;
> _ConnectionPtr global_conn = NULL;
> _ConnectionPtr conn = NULL;
> _CommandPtr cmd = NULL;
> BOOL fCreateTestTable = TRUE;
> int i;
> CoInitialize(NULL);
>
> try
> {
> // Open global conn to keep Jet loaded (perf).
> global_conn = GetJetConnection();
>
> for (i=1; i<=LOOP_COUNT; i++ )
> {
> // Open local conn for loop.
> conn = GetJetConnection();
>
> if ( fCreateTestTable )
> {
> try
> {
> conn->Execute(L"drop table T1",NULL,adExecuteNoRecords|adCmdText);
> }
> catch ( _com_error ex )
> {
>
> }
>
> conn->Execute(L"create table T1(f1 int, f2
> text(255))",NULL,adExecuteNoRecords|adCmdText);
> fCreateTestTable = FALSE;
> }
>
> // Now create parameterized ADO call.
> hr = cmd.CreateInstance( __uuidof(Command) );
> if ( FAILED( hr ) ) throw( _com_error( hr, NULL ) );
>
> cmd->ActiveConnection = conn;
>
> // Create insert statement and setup params.
> cmd->CommandText = L"insert into T1 (f1,f2) values (?,?)";
> cmd->CommandType = adCmdText;
>
> cmd->Parameters->Append(cmd->CreateParameter(
> L"p1",adInteger,adParamInput,0));
> cmd->Parameters->Append(cmd->CreateParameter(
> L"p2",adVarChar,adParamInput,255));
>
> cmd->Parameters->Item[0L]->Value = 1L;
> cmd->Parameters->Item[1L]->Value = L"This is a test";
>
> // Execute statement.
> cmd->Execute(NULL,NULL,adExecuteNoRecords);
>
> // Cleanup
> cmd->ActiveConnection = NULL;
> cmd = NULL;
> conn->Close();
> conn = NULL;
>
> if ( 0 == (i%1000))
> {
> printf( "Completed %08lu of %08lu iterations.\n", i, LOOP_COUNT );
> }
> }
>
> }
> catch( _com_error ex )
> {
> // Robust error trapping left as an exercise for the reader. (G)
> printf( "_com_error=%S\n" );
> }
> return;
> }
> // END SAMPLE
>
>
> Matt Neerincx [MSFT]
>
> This posting is provided "AS IS", with no warranties, and confers no rights.
>
> Please do not send email directly to this alias. This alias is for newsgroup
> purposes only.
>
> "khalprin" <khalprin@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:88157683-9E86-44B2-B65E-6C040A959E72@xxxxxxxxxxxxxxxx
> > My logic is such that the connection to the database is not maintained
> > between calls to the Execute() method. Sorry I didn't mention that
> > before.
> > My test involves these steps:
> >
> > 1. Create the connection object
> > 2. Connect to the database
> > 3. Create the command object
> > 4. Create/Append command parameters
> > 5. Execute the command
> > 6. Destroy the command object
> > 7. Close database connection
> > 8. Go to step 2 for xxxx iterations
> > 9. Destroy the connection object.
> >
> > Thanks.
> >
> >
> > "Val Mazur (MVP)" wrote:
> >
> >> Hi,
> >>
> >>
> >> See if it does not help
> >>
> >> http://support.microsoft.com/default.aspx?scid=kb;en-us;248014
> >>
> >> --
> >> Val Mazur
> >> Microsoft MVP
> >>
> >> http://xport.mvps.org
> >>
> >>
> >>
> >> "khalprin" <khalprin@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> >> news:ACD0680A-59E1-4179-BEA8-1AFF685C6441@xxxxxxxxxxxxxxxx
> >> > Hello,
> >> >
> >> > I'm tracking down a memory leak and have narrowed it down to the single
> >> > line
> >> > of my code that calls the Execute() method of the ADODB Command object.
> >> >
> >> > In this case, the Execute() is executing a query (that takes 2
> >> > parameters)
> >> > in MS Access that inserts 3 records into a table. I'm using the
> >> > command
> >> > 'adCmdStoredProc' with the flag 'adExecuteNoRecords'. The command
> >> > executes
> >> > successfully and does not return a recordset pointer.
> >> >
> >> > When I run this through 1000s of iterations for my test, memory usage
> >> > grows
> >> > continuously. If I comment out the Execute() call, memory usage does
> >> > not
> >> > grow.
> >> >
> >> > The command is setup as follows:
> >> >
> >> > pCmd->put_ActiveConnection(*pConn); // variant
> >> > pCmd->CommandText = szQuery; // name of query/stored proc
> >> > pCmd->CommandType = cmd; // ADODB command id
> >> >
> >> > The actual line of code that executes the command is:
> >> >
> >> > pCmd->Execute(NULL, NULL, cmd | ADODB::adExecuteNoRecords);
> >> >
> >> >
> >> > Any ideas?
> >> >
> >>
> >>
> >>
>
>
>
.



Relevant Pages

  • Re: ADODB Command memory leak
    ... _ConnectionPtr conn = NULL; ... // Execute statement. ... > My logic is such that the connection to the database is not maintained ... Create/Append command parameters ...
    (microsoft.public.data.ado)
  • Re: SQLConnection and Pooling
    ... I disagree, the code is not fine, connection close is not guaranteed. ... NOT leak! ... Dim conn As New SqlConnection ... > Public Shared Sub MainAs String) ...
    (microsoft.public.dotnet.framework.adonet)
  • ado memory leak? - source provided
    ... I'm trying to find a memory leak in my code that uses ADO and have a sample ... // Open global conn to keep Jet loaded. ... #ifdef USE_CMDPROC ...
    (microsoft.public.vc.database)
  • Re: Memory Leak in managed Code
    ... >> I'm running this on v1.1 and v2.0 without any leak at all. ... >> But I do have a connection with an SQL server on another Server using ... >> I guess that the OP's issue relates to the local connection using shared ... >> TCP channels. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Timeout for connect()
    ... Don't I run the risk of some kind of leak? ... socket that hasn't been opened? ... >> connection context and not create a new one? ... so I wanted to be able to cancel the connect ...
    (comp.unix.programmer)