Re: ADODB Command memory leak



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

  • [Full-Disclosure] Advanced usage of system() function.
    ... and call its arguments as a command for shell. ... as we can see we still didnt get what we want (typing exit ... Connection closed by foreign host. ... think what we want to execute. ...
    (Full-Disclosure)
  • Advanced usage of system() function.
    ... and call its arguments as a command for shell. ... as we can see we still didnt get what we want (typing exit we are ... Connection closed by foreign host. ... think what we want to execute. ...
    (Bugtraq)
  • Re: sp_help_job (HELP)
    ... it's defintely the Fill line that's causing this right? ... > The following code produces a "severe error occurred on the command ... problem (using the same connection id and pwd). ... I've tried to execute it ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: ADODB Command memory leak
    ... My logic is such that the connection to the database is not maintained ... between calls to the Execute() method. ... Create/Append command parameters ... Destroy the command object ...
    (microsoft.public.data.ado)
  • Re: ADODB Command memory leak
    ... I ran it and indeed it does not leak memory. ... single Command Execute line stops the leak?? ... > // Open global conn to keep Jet loaded. ... >> My logic is such that the connection to the database is not maintained ...
    (microsoft.public.data.ado)