🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
3 messages
3 total messages Started by Dan Mon, 13 Aug 2007 19:11
std::auto_ptr<> Memory Leak?
#1490
Author: Dan
Date: Mon, 13 Aug 2007 19:11
32 lines
2019 bytes
I have the following code:

std::auto_ptr<TStringList> Times(new TStringList());
for (int i=0;i<100; i++)
{
   String StartTime = Now();
   std::auto_ptr<TADOQuery> Query(new TADOQuery(this));
   Query->Connection = Database;
   Query->SQL->Text = "Select * from Customers where Cust_Id = 
"+(String)i; //yes, each customer has the same amount of information
   Query->Active = true;
   String StopTime = Now();
   Times->Add(StartTime+"\t"+EndTime);
}
ShowMessage(Times->Text);

I'm confused why the execution time grows each time through the loop as 
shown in the message at the end.  Of course, I could move the creation 
of the object 'Query' outside of the loop; however, that defeats the 
function I wanted; in my main code I have a similar loop that creates an 
object (say bob) that then creates that query (without a loop) and it 
appears as though as the number of times I create the 'bob' object the 
slower the system gets and the above code snip demonstrates the same effect.

Ideas?


The other reason I posted the above here for help is because if I watch 
the task manager, the amount of memory listed on the processes tab for 
this process grows while in the loop, which I wouldn't expect.

Thoughts?
Re: std::auto_ptr<> Memory Leak?
#1491
Author: Thomas Maeder [TeamB]
Date: Mon, 13 Aug 2007 20:04
18 lines
1673 bytes
Dan <Dan@do.not.spam> writes:

> I have the following code:
>
> std::auto_ptr<TStringList> Times(new TStringList());
> for (int i=0;i<100; i++)
> {
>    String StartTime = Now();
>    std::auto_ptr<TADOQuery> Query(new TADOQuery(this));
>    Query->Connection = Database;
>    Query->SQL->Text = "Select * from Customers where Cust_Id = "+(String)i; //yes, each customer has the same amount of information
>    Query->Active = true;
>    String StopTime = Now();
>    Times->Add(StartTime+"\t"+EndTime);

Are you sure that this is the code? EndTime isn't declared at this point.

> }
Re: std::auto_ptr<> Memory Leak?
#1492
Author: Dan
Date: Mon, 13 Aug 2007 20:16
54 lines
3013 bytes
> Are you sure that this is the code? EndTime isn't declared at this point.

You are correct, no this isn't the 'exact code'  EndTime should be 
StopTime.  I've ran another test more straightforward test with the 
following code:

void TForm1::TestFunctionButtonClick(TObject *Sender)
{
    std::auto_ptr<TStringList> Times(new TStringList());
    const int Number_Of_Loops = 100;
    for (int i=1; i<Number_Of_Loops; i++)
    {
     const String Time1 = GetTimeMessage();

     std::auto_ptr<TADOQuery> Query(new TADOQuery(this));
     const String Time2 = GetTimeMessage();

     Query->Connection = Database;
     Database->Connected = true;
     const String Time3= GetTimeMessage();

    const String SQL = "Select * from Samples where Sample_ID = 
"+(String)i+" and Sub_Sample_ID = 1";
     Query->SQL->Text = SQL;

     Query->Active = true;
     const String Time4= GetTimeMessage();
 
Times->Add((String)MillisBetween(Time1,Time2)+"\t"+(String)MillisBetween(Time2,Time3)+"\t"+(String)MillisBetween(Time3,Time4));

    }
    ShowReadOnlyInformation("Times",Times->Text);
}


The upside is that I don't see performance drags between creation of the 
auto_ptrs (Time 1 and Tim2e for example).

I do see the 'processes' memory number increase while the loop is going 
on; and I do see that the SQL->Active call (Time 3 to Time 4) is almost 
completely random.  While I don't see a huge trend line upwords as I 
initially thought, I do see that a handful of the 100 SQL->Active calls 
sometimes take over 4 seconds to complete, vs the average is around 
100ms; and the number of calls that take much more than the average time 
increases as I get further in the loop.

And no, on subsequent calls the select statements that are slower are 
not for the same records, or in otherwords one call that took 4 seconds 
on one run may take 100ms on the next and vice versa.  The database is a 
local Access database.

So I think I have a memory issue, looking at the task manager, but I am 
more confused why the turn around times for SQL->Active aren't consistent.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads