Use of TMSStoredProc

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
joepasquariello
Posts: 38
Joined: Wed 16 May 2007 01:12

Use of TMSStoredProc

Post by joepasquariello » Fri 04 Jan 2008 00:23

I've got a memory leak somewhere, and I'm wondering if I get some help from you folks. Am I using TMSStoredProc correctly? In the code below, RTMetSP points to a TMSStoredProc object that is created at runtime. Each time the code below is executed, Params->ParamValues[] are updated, and the stored procedure is executed. Does this code look okay? Do Params need to be "cleared" between executions? Is there anything that can happen inside a stored procedure that will create a memory leak in a calling application?

Thanks very much,

Joe

TMSStoredProc *SP = RTMetSP; // pointer to stored procedure
TParams *P = SP->Params; // pointer to Params property

P->ParamValues["Device"] = Label;
P->ParamValues["DateTimeStamp"] = Now();
P->ParamValues["CommError"] = CommError;
P->ParamValues["OpStatus"] = OpStatus;
P->ParamValues["DeviceTimeStamp"] = DeviceDateTime;

P->ParamValues["Wspd1"] = Fmet->TenMin.Eng.Wspd1_1s;
P->ParamValues["Wspd2"] = Fmet->TenMin.Eng.Wspd2_1s;
P->ParamValues["Wspd3"] = Fmet->TenMin.Eng.Wspd3_1s;
P->ParamValues["Wspd4"] = Fmet->TenMin.Eng.Wspd4_1s;
P->ParamValues["Wdir1"] = Fmet->TenMin.Eng.Wdir1_1s;
P->ParamValues["Wdir2"] = Fmet->TenMin.Eng.Wdir2_1s;
P->ParamValues["Temp1"] = Fmet->TenMin.Eng.Temp1_1s;
P->ParamValues["Temp2"] = Fmet->TenMin.Eng.Temp2_1s;
P->ParamValues["Baro"] = Fmet->TenMin.Eng.Baro1_1s;

try { // test for C++ exceptions
SP->ExecProc(); // execute stored procedure
}
catch ( ... ) {
// handler for any C++ exception
}
}

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Fri 04 Jan 2008 09:42

Your code looks ok. Does the memory leak on each stored procedure execution, or only on executions that raise errors? What tool do you use to detect the memory leak.
Could you send me a complete small sample at sdac*crlab*com to demonstrate it, including script to create server objects?

Also supply me the following information:
- exact version of SDAC. You can see it in the About sheet of TMSConnection Editor;
- exact version of your IDE;
- exact version of SQL server and client. You can see it in the Info sheet of TMSConnection Editor.

joepasquariello
Posts: 38
Joined: Wed 16 May 2007 01:12

Post by joepasquariello » Sat 05 Jan 2008 19:36

Antaeus,

I'm not sure there is a memory leak, and if there is a memory leak, I'm not sure where it is. I'm fairly new to database stuff, so I needed to ask if my code looked okay. The application _sometimes_ acts as if there is a memory leak, but not all of the time. It's very strange. I will do some testing of the stored procedure calls in a small test program and I'll let you know how that looks.

Joe

joepasquariello
Posts: 38
Joined: Wed 16 May 2007 01:12

Post by joepasquariello » Wed 09 Jan 2008 17:28

Antaeus,

What I found is that the function that executes the stored procedure was being called many times, and sometimes was failing due to a "duplicate primary key". Do you understand why this would cause a memory leak? My next step is to create a test program I can send to you.

Joe

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Fri 11 Jan 2008 08:55

Probably this is a bug of C++Builder. Please take a look at this thread for more information.

joepasquariello
Posts: 38
Joined: Wed 16 May 2007 01:12

Post by joepasquariello » Fri 11 Jan 2008 15:22

Thanks very much. I think that explains the problem exactly.

I am using RAD Studio 2007 with the latest updates, so this problem exists all the way up to the latest version of C++ Builder.

Joe

joepasquariello
Posts: 38
Joined: Wed 16 May 2007 01:12

Post by joepasquariello » Fri 11 Jan 2008 17:11

Using (Exception &E) still does not solve the memory leak in my case. I'm using RAD2007 with latest patches. The code now looks as shown below. If I call this code many times with the same data, there is a duplicate record exception on each call after the first one. The memory leak is the same with (...) and with (Exception &E).

Joe

try { // test for C++ exceptions
SP->ExecProc(); // execute stored procedure
}
catch (Exception &E) { // (...) causes memory leak
// handler for any C++ exception
}

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Mon 14 Jan 2008 08:28

Please try to prepare a complete small sample and send it to sdac*crlab*com, including script to create and fill table. Also specify the exact version of your SDAC, and what tool do you use to detect the memory leak.

joepasquariello
Posts: 38
Joined: Wed 16 May 2007 01:12

Post by joepasquariello » Mon 14 Jan 2008 22:10

Hello Antaeus,

I don't know how to provide that type of script. I'm using an SQL Server database developed by others, and I really know very little about databases. The exception that occurs is a "duplicate primary key" exception. Perhaps it would be adequate for you to call any stored procedure that inserts a record, but fails unless the primary key is unique. At this point, I'm assuming this is a RAD2007 issue, and not an SDAC issue.

Joe

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Wed 16 Jan 2008 11:38

Try to modify your code so that it looks like this:

Code: Select all

try 
{
  SP->ExecProc();
}
  catch (EMSError &E) { }   //   <<---  the new line
  catch (Exception &E) { }
}

joepasquariello
Posts: 38
Joined: Wed 16 May 2007 01:12

Post by joepasquariello » Wed 16 Jan 2008 16:22

Hello Antaeus,

Adding that line solves the problem. Do I need both catch clauses? Can you please explain what is the problem and why this fixes it?

Thanks very much.

Joe

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Thu 17 Jan 2008 08:39

The first catch clause is required to catch only errors of the EMSError type to avoid the memory leak. The second one is required to catch other exceptions.
It looks like a bug in C++Builder with handling exceptions raised in Delphi packages.

joepasquariello
Posts: 38
Joined: Wed 16 May 2007 01:12

Post by joepasquariello » Thu 17 Jan 2008 13:40

hi antaeus,

are you saying it should not be necessary to do that to avoid the leak? if so, that's a serious bug and you should report it to codegear. i would do it myself, but i don't understand the issue well enough. if you look at the c++ builder IDE newsgroup, you'll see a "try / catch memory leak" thread, started by me, and you can reply.

thanks for your help.

joe

joepasquariello
Posts: 38
Joined: Wed 16 May 2007 01:12

Post by joepasquariello » Tue 29 Jan 2008 00:32

Antaeus,

The code you suggested is shown below. Do the two "catch" clauses act like "if/else"? What I mean is, does only one catch clause get executed for any exception? Can the final catch clause use the syntax "(...)"?

Joe

try {
SP->ExecProc();
}
catch (EMSError &E) {
}
catch (Exception &E) {
}

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Tue 29 Jan 2008 08:47

All exceptions of the EMSError class will be handled in the first catch{} section. All other errors will be handles in the second catch{} section. Thus each error will be handled only once.
Last edited by Antaeus on Fri 01 Feb 2008 09:16, edited 1 time in total.

Post Reply