Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

<b>Memory usage grows in BCB5 app</b>

Status
Not open for further replies.

maj12

Programmer
Nov 14, 2000
7
CA
Hi there,

I've developed an app in BCB5 that queries a SQL database and an Access database every 60 seconds to see if there are any reports due to run. The problem is, every time the timer fires and the code is run, the memory usage increases from 70K to 200K. I am new to C++ in general so can anyone tell me if I am not closing or deleting the proper objects. I won;t post all the code cause it's long - just the pertinent stuff. Also, I don't know what this means but if I minimize the app then maximize it again, the memory jumps back down to what it started at (approx 5MB). Thanks:

//only check DocuCentral reports if .ini exists
//these reports are stored in SQL database
if(dcExists==true){
//Get DSN info from DCOptions.ini
AnsiString tmpDSN;
AnsiString tmpUserID;
AnsiString tmpPassword;
AnsiString tmpIC;
AnsiString dcRoot;
AnsiString iniLoc = ExtractFileDir(ParamStr(0))+"\\DCOptions.ini";
AnsiString homeDir = ExtractFileDir(ParamStr(0));
TIniFile *ini;
ini = new TIniFile( iniLoc );
tmpDSN = ini->ReadString ( "DocuCentral DB Settings", "DSN", "" );
tmpUserID = Decrypt(ini->ReadString ( "DocuCentral DB Settings", "User ID", "" ).c_str());
tmpPassword = Decrypt(ini->ReadString ( "DocuCentral DB Settings", "Password", "" ).c_str());
tmpIC = ini->ReadString ( "DocuCentral DB Settings", "Initial Catalog", "");
dcRoot = ini->ReadString ( "DocuCentral DB Settings", "Root Dir", "");
delete ini;

//See if any reports are due to run
TCOM_Recordset RsTemp = CoRecordset::Create();
TCOM_Recordset RsTemp2 = CoRecordset::Create();
TCOM_Recordset RsTemp3 = CoRecordset::Create();
TCOM_Recordset RsTemp4 = CoRecordset::Create();

//check Every Reports
RsTemp->Open(
WideString(&quot;SELECT * FROM Reports_Scheduled_Every WHERE Start_Date <= GetDate() AND End_Date >= GetDate()&quot;).Detach(),
WideString(&quot;Provider=SQL OLEDB;User ID=&quot;+tmpUserID.Trim()+&quot;;Password=&quot;+tmpPassword.Trim()+&quot;;Initial Catalog=&quot;+tmpIC.Trim()+&quot;;Data Source=&quot;+tmpDSN.Trim()).Detach(),
Adodb_tlb::adOpenKeyset,
Adodb_tlb::adLockOptimistic,
Adodb_tlb::adCmdText
);

while ( !bool(RsTemp->EOF) )
{
if(StrToDateTime(RsTemp->Fields->get_Item(6)->get_Value()) + Double(RsTemp->Fields->get_Item(5)->get_Value()) <= Now())
{
AnsiString index = RsTemp->Fields->get_Item(0)->get_Value();
AnsiString rptName = Trim(RsTemp->Fields->get_Item(1)->get_Value());
AnsiString newExt = &quot;_&quot;+FormatDateTime(&quot;mmddyyyy_hhmm&quot;, Now());
AnsiString saveName;
AnsiString jobName = Trim(RsTemp->Fields->get_Item(9)->get_Value());
AnsiString email = Trim(RsTemp->Fields->get_Item(10)->get_Value());
TCrExportType exportType;
int isCustom = 0;
if (RsTemp->Fields->get_Item(7)->get_Value() == true)
{
isCustom = 1;
}

switch (StrToInt(Trim(RsTemp->Fields->get_Item(8)->get_Value())))
{
case 1:
exportType = ExcelXLS;
saveName = jobName+newExt+&quot;.xls&quot;;
break;
case 2:
exportType = HTML4;
saveName = jobName+newExt+&quot;.htm&quot;;
break;
case 3:
exportType = AdobeAcrobatPDF;
saveName = jobName+newExt+&quot;.pdf&quot;;
break;
case 4:
exportType = WordForWindows;
saveName = jobName+newExt+&quot;.doc&quot;;
break;
}
CrScheduler->ReportName = dcRoot+&quot;\\Reports\\&quot;+rptName;
CrScheduler->Output = toExport;
CrScheduler->ExportOptions->FileType = exportType;
CrScheduler->ExportOptions->Destination = toFile;
CrScheduler->ExportOptions->FileName = dcRoot+&quot;\\Reports\\&quot;+saveName;

//get Parameters, if any
RsTemp3->Open(
WideString(&quot;SELECT * FROM Params_Reports_Every WHERE Reports_Scheduled_Every_Index = &quot;+RsTemp->Fields->get_Item(0)->get_Value()+&quot; ORDER BY Param_Index&quot;).Detach(),
WideString(&quot;Provider=SQL OLEDB;User ID=&quot;+tmpUserID.Trim()+&quot;;Password=&quot;+tmpPassword.Trim()+&quot;;Initial Catalog=&quot;+tmpIC.Trim()+&quot;;Data Source=&quot;+tmpDSN.Trim()).Detach(),
Adodb_tlb::adOpenKeyset,
Adodb_tlb::adLockPessimistic,
Adodb_tlb::adCmdText
);
int paramNum = 0;
while (!bool(RsTemp3->EOF))
{
CrScheduler->ParamFields->Items[paramNum]->CurrentValue=Trim(RsTemp3->Fields->get_Item(4)->get_Value());
paramNum++;
RsTemp3->MoveNext();
}
RsTemp3->Close();

//run report
Memo1->Lines->Add(FormatDateTime(&quot;mm/dd/yyyy hh:mm:ss&quot;, Now())+&quot; - &quot;+jobName+&quot; - &quot;+rptName+&quot; - &quot;+saveName);
WriteLog(FormatDateTime(&quot;mm/dd/yyyy hh:mm:ss&quot;, Now())+&quot; - &quot;+jobName+&quot; - &quot;+rptName+&quot; - &quot;+saveName);
CrScheduler->Execute();

//add report to Documents
RsTemp3->Open(
WideString(&quot;INSERT INTO Documents VALUES('&quot;+saveName+&quot;', '&quot;+jobName+&quot;',&quot;+isCustom+&quot;)&quot;).Detach(),
WideString(&quot;Provider=SQL OLEDB;User ID=&quot;+tmpUserID.Trim()+&quot;;Password=&quot;+tmpPassword.Trim()+&quot;;Initial Catalog=&quot;+tmpIC.Trim()+&quot;;Data Source=&quot;+tmpDSN.Trim()).Detach(),
Adodb_tlb::adOpenKeyset,
Adodb_tlb::adLockPessimistic,
Adodb_tlb::adCmdText
);
RsTemp3->Close();

//add permissions if custom
if(isCustom == 1)
{
//get index of record just added
RsTemp3->Open(
WideString(&quot;SELECT Documents_Index FROM Documents WHERE Name = '&quot;+saveName+&quot;'&quot;).Detach(),
WideString(&quot;Provider=SQL OLEDB;User ID=&quot;+tmpUserID.Trim()+&quot;;Password=&quot;+tmpPassword.Trim()+&quot;;Initial Catalog=&quot;+tmpIC.Trim()+&quot;;Data Source=&quot;+tmpDSN.Trim()).Detach(),
Adodb_tlb::adOpenKeyset,
Adodb_tlb::adLockPessimistic,
Adodb_tlb::adCmdText
);
//get permissions from Groups_Scheduled_Every_Permissions
RsTemp2->Open(
WideString(&quot;SELECT Groups_Index FROM Groups_Scheduled_Every_Permissions WHERE Reports_Scheduled_Every_Index =&quot;+index ).Detach(),
WideString(&quot;Provider=SQL OLEDB;User ID=&quot;+tmpUserID.Trim()+&quot;;Password=&quot;+tmpPassword.Trim()+&quot;;Initial Catalog=&quot;+tmpIC.Trim()+&quot;;Data Source=&quot;+tmpDSN.Trim()).Detach(),
Adodb_tlb::adOpenKeyset,
Adodb_tlb::adLockPessimistic,
Adodb_tlb::adCmdText
);
//add permissions to Groups_Documents_Permissions if Custom
while (!bool(RsTemp2->EOF))
{
RsTemp4->Open(
WideString(&quot;INSERT INTO Groups_Documents_Permissions VALUES(&quot;+RsTemp2->Fields->get_Item(0)->get_Value()+&quot;, &quot;+RsTemp3->Fields->get_Item(0)->get_Value()+&quot;)&quot;).Detach(),
WideString(&quot;Provider=SQL OLEDB;User ID=&quot;+tmpUserID.Trim()+&quot;;Password=&quot;+tmpPassword.Trim()+&quot;;Initial Catalog=&quot;+tmpIC.Trim()+&quot;;Data Source=&quot;+tmpDSN.Trim()).Detach(),
Adodb_tlb::adOpenKeyset,
Adodb_tlb::adLockPessimistic,
Adodb_tlb::adCmdText
);
RsTemp2->MoveNext();
}
RsTemp2->Close();
RsTemp3->Close();
RsTemp4->Close();
}

//email if necessary
if(email != &quot;None&quot;)
{
CrScheduler->ExportOptions->Destination = toEmailViaMapi;
CrScheduler->ExportOptions->Email->ToList = email;
CrScheduler->ExportOptions->Email->Subject=&quot;Scheduled Report - &quot;+saveName;
CrScheduler->Execute();
}

//update last_run
RsTemp2->Open(
WideString(&quot;UPDATE Reports_Scheduled_Every SET Last_Run = '&quot;+Now()+&quot;' WHERE Reports_Scheduled_Every_Index = &quot;+index).Detach(),
WideString(&quot;Provider=SQL OLEDB;User ID=&quot;+tmpUserID.Trim()+&quot;;Password=&quot;+tmpPassword.Trim()+&quot;;Initial Catalog=&quot;+tmpIC.Trim()+&quot;;Data Source=&quot;+tmpDSN.Trim()).Detach(),
Adodb_tlb::adOpenKeyset,
Adodb_tlb::adLockPessimistic,
Adodb_tlb::adCmdText
);
RsTemp2->Close();
}
RsTemp->MoveNext();
}
RsTemp->Close();

//check first/last reports
{
<b>snip checking of 2 more report types in SQL DB></b>
}
//when done checking reports and exporting
RsTemp->Close();

delete RsTemp;
delete RsTemp2;
delete RsTemp3;
delete RsTemp4;
CrScheduler->Clear();
}
//begin checking normal reports
//these are stored in Access DB

TCOM_Recordset RsTemp = CoRecordset::Create();
TCOM_Recordset RsTemp2 = CoRecordset::Create();

//check Every Reports
RsTemp->Open(
WideString(&quot;SELECT * FROM Reports_Every WHERE Start_Date <= Now() AND End_Date >= Now()&quot;).Detach(),
WideString(&quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot;+ExtractFileDir(ParamStr(0))+&quot;\\dcScheduler.mdb;Persist Security Info = False&quot;).Detach(),
Adodb_tlb::adOpenKeyset,
Adodb_tlb::adLockOptimistic,
Adodb_tlb::adCmdText
);
while ( !bool(RsTemp->EOF) )
{
if(StrToDateTime(RsTemp->Fields->get_Item(6)->get_Value()) + Double(RsTemp->Fields->get_Item(8)->get_Value()) <= Now())
{
AnsiString index = RsTemp->Fields->get_Item(0)->get_Value();
AnsiString path = Trim(RsTemp->Fields->get_Item(1)->get_Value());
AnsiString jobName = Trim(RsTemp->Fields->get_Item(2)->get_Value());
AnsiString newExt = &quot;_&quot;+FormatDateTime(&quot;mmddyyyy_hhmm&quot;, Now());
AnsiString savePath = Trim(RsTemp->Fields->get_Item(3)->get_Value());
AnsiString email = Trim(RsTemp->Fields->get_Item(9)->get_Value());
int format = RsTemp->Fields->get_Item(7)->get_Value();
AnsiString saveName;
TCrExportType exportType;
switch (format)
{
case 1:
exportType = ExcelXLS;
saveName=savePath+&quot;\\&quot;+jobName+newExt+&quot;.xls&quot;;
break;
case 2:
exportType=HTML4;
saveName=savePath+&quot;\\&quot;+jobName+newExt+&quot;.htm&quot;;
break;
case 3:
exportType=AdobeAcrobatPDF;
saveName=savePath+&quot;\\&quot;+jobName+newExt+&quot;.pdf&quot;;
break;
case 4:
exportType=WordForWindows;
saveName=savePath+&quot;\\&quot;+jobName+newExt+&quot;.doc&quot;;
break;
}
Cr1->ReportName = path;
Cr1->Output = toExport;
Cr1->ExportOptions->FileType = exportType;
Cr1->ExportOptions->Destination = toFile;
Cr1->ExportOptions->FileName = saveName;

//get Parameters, if any
RsTemp2->Open(
WideString(&quot;SELECT * FROM Params_Every WHERE Reports_Every_Index = &quot;+index+&quot; ORDER BY Param_Index&quot;).Detach(),
WideString(&quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot;+ExtractFileDir(ParamStr(0))+&quot;\\dcScheduler.mdb;Persist Security Info = False&quot;).Detach(),
Adodb_tlb::adOpenKeyset,
Adodb_tlb::adLockPessimistic,
Adodb_tlb::adCmdText
);
int paramNum = 0;
while (!bool(RsTemp2->EOF))
{
Cr1->ParamFields->Items[paramNum]->CurrentValue=Trim(RsTemp2->Fields->get_Item(4)->get_Value());
paramNum++;
RsTemp2->MoveNext();
}
RsTemp2->Close();

//run report
Memo1->Lines->Add(FormatDateTime(&quot;mm/dd/yyyy hh:mm:ss&quot;, Now())+&quot; - &quot;+jobName+&quot; - &quot;+path+&quot; - &quot;+saveName);
WriteLog(FormatDateTime(&quot;mm/dd/yyyy hh:mm:ss&quot;, Now())+&quot; - &quot;+jobName+&quot; - &quot;+path+&quot; - &quot;+saveName);
Cr1->Execute();

//email if necessary
if(email != &quot;None&quot;)
{
Cr1->ExportOptions->Destination = toEmailViaMapi;
Cr1->ExportOptions->Email->ToList = email;
Cr1->ExportOptions->Email->Subject=&quot;Scheduled Report - &quot;+saveName;
Cr1->Execute();
}

//update last_run
RsTemp2->Open(
WideString(&quot;UPDATE Reports_Every SET Last_Run = '&quot;+Now()+&quot;' WHERE Reports_Every_Index = &quot;+index).Detach(),
WideString(&quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot;+ExtractFileDir(ParamStr(0))+&quot;\\dcScheduler.mdb;Persist Security Info = False&quot;).Detach(),
Adodb_tlb::adOpenKeyset,
Adodb_tlb::adLockPessimistic,
Adodb_tlb::adCmdText
);
RsTemp2->Close();
}
RsTemp->MoveNext();
}
RsTemp->Close();

//check Nth reports
{
<b><snip checking of 2 more report types></b>
}
RsTemp->Close();

delete RsTemp;
delete RsTemp2;
Cr1->Clear();
bttnMainStop->Enabled = true;
 
I've not analyzed your code but I've run across an article in community.borland.com about &quot;Maximizing Peformance of Delphi/C++ Builder/InterBase Apps.&quot; It says that if you close your transactions, the memory will be released. Don't hold your transactions open for long periods of time.

Try closing your transactions and see what happens.
James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
you got a memory leak somewhere. make sure you close all files after you open them. open the file, do what you gotta do, and close it asap. always try to keep the file open as little as you possibly can. if in the off-chance you don't know how to close the file, try fclose(*FILE) or FileClose(*FILE). just in case.

Cyprus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top