Article View: borland.public.cppbuilder.database.ado
Article #159Re: TADOQuery Memory Leak
From: Nige
Date: Wed, 25 Apr 2007 13:01
Date: Wed, 25 Apr 2007 13:01
137 lines
4790 bytes
4790 bytes
Hi Patrick,
Memory management in BCB is very bad. Bytes are leaked here and there
throughout the running of any BCB application.
You can minimise the problem by creating as few objects as needed. So could
your TADOConnection and TADOQuery be created outside of this thread object,
and only pointers to these objects passed in? This will help your memory
problem a lot.
However I'm not sure how this will affect things since you are using a
thread. You might have to synchronise access to the query and connection so
your main thread doesn't use these at the same time as the new thread.
Nigel
PS To tidy up your code a bit, you don't have to call Close() before
deleting your TADOQuery. Also, it's quicker to say query->SQL->Text =
"select..." instead of query->SQL->Clear() then
query->SQL->Add("select...").
"Patrick Wong" <patrickwong@computer.org> wrote in message
news:462d7a19$1@newsgroups.borland.com...
> Dear all,
>
> Does anyone ever experience the captioned problem? My application is
having
> this problem, after being run for several hours the memory usage grew
> significantly, error pops up with "Virtual memory too low" and the app has
> to be restarted again. The ADOQuery is created and run in a thread. Code
> excerpts are something like below:
>
> __fastcall TThreadTestDB::TThreadTestDB(bool CreateSuspended)
> : TThread(CreateSuspended)
> {
> m_ADOConnectionTest = new TADOConnection(NULL);
> m_ADOConnectionTest->LoginPrompt = false;
> m_ADOConnectionTest->ConnectionString =
> AnsiString("Provider=Ifxoledbc.2;Password=bar;Persist Security
> Info=True;User ID=foo;Data Source=abc@dbserver");
> m_ADOQueryTest = new TADOQuery(NULL);
> m_ADOQueryTest->Connection = m_ADOConnectionTest;
> }
>
//--------------------------------------------------------------------------
-
>
> void __fastcall TThreadTestDB::Execute()
> {
> AnsiString strSQL = "select name from table1 where account=\'1234\'";
> AnsiString strRtn;
>
> //---- Place thread code here ----
> CoInitialize(NULL);
>
> if(!m_ADOConnectionTest->Connected)
> m_ADOConnectionTest->Open();
>
> do
> {
> if(m_ADOQueryTest->Active)
> m_ADOQueryTest->Close();
>
> m_ADOQueryTest->SQL->Clear();
> m_ADOQueryTest->SQL->Add(strSQL);
> m_ADOQueryTest->Prepared = true;
> m_ADOQueryTest->Open();
>
> if(m_ADOQueryTest->RecordCount > 0)
> {
> m_ADOQueryTest->First();
>
> do
> {
> strRtn = m_ADOQueryTest->FieldByName("name")->AsString;
> m_ADOQueryTest->Next();
> } while(!m_ADOQueryTest->Eof);
> }
> }
> ::Sleep(100);
> Application->ProcessMessages();
> } while (!Terminated);
>
> if(m_ADOQueryTest->Active)
> m_ADOQueryTest->Close();
>
> CoUninitialize();
> }
>
//--------------------------------------------------------------------------
-
>
> __fastcall TThreadTestDB::~TThreadTestDB(void)
> {
> if(m_ADOQueryTest != NULL)
> {
> m_ADOQueryTest->Close();
> delete m_ADOQueryTest;
> m_ADOQueryTest = NULL;
> }
>
> if(m_ADOConnectionTest != NULL)
> {
> m_ADOConnectionTest->Close();
> delete m_ADOConnectionTest;
> m_ADOConnectionTest = NULL;
> }
> }
>
>
> Each time the m_ADOQueryTest->Open(); is executed the memory usage
increases
> by 10-20k, without ever drops when the query is closed. I also tried
> deleting the and recreating the ADOQuery in every iteration in the
do..while
> loop, but it does not help.
>
>
> Googling can find many threads discussing this problem, one of which is:
>
>
http://groups.google.de/group/borland.public.delphi.thirdpartytools.general/feed/atom_v1_0_topics.xml?num=50
>
> But most of them ends without concrete solution.
>
> I tested in both BCB6 and BDS2006 with most updated patches. I am running
> the app in Windows XP.
>
>
> Hope can get some kind advice here.
>
>
> Cheers,
> Patrick
>
>
Technical Details
Message-ID:
<462f510d$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<462d7a19$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10698b,f6b9f007fd37682e X-Google-Attributes: gid10698b,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Nige" <dont@email.me> Newsgroups: borland.public.cppbuilder.database.ado References: <462d7a19$1@newsgroups.borland.com> Subject: Re: TADOQuery Memory Leak Date: Wed, 25 Apr 2007 14:01:02 +0100 Lines: 139 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1807 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1807 NNTP-Posting-Host: 62.173.124.106 Message-ID: <462f510d$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1177506061 62.173.124.106 (25 Apr 2007 06:01:01 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail