Thread View: borland.public.cppbuilder.nativeapi
6 messages
6 total messages
Started by Mark Guerrieri
Thu, 03 Apr 2003 08:40
Ternary operator memory leak
Author: Mark Guerrieri
Date: Thu, 03 Apr 2003 08:40
Date: Thu, 03 Apr 2003 08:40
36 lines
2238 bytes
2238 bytes
Using BCB6 on Win2K all SP's applied to everything...
In an NT service, I have a thread which loops about 10 times a second and in
the loop is the following code:
...
AnsiString cOp = FQuery->FieldByName("operator")->AsString.Trim();
AnsiString cUserName = (cOp.Length() > 0) ?
FQuery->FieldByName("tsr")->AsString.Trim() : AnsiString("");
...
Where FQuery is a dynamically created at runtime TTable (I have also tried
TADOTable and TADOQuery, but I don't think this is the issue).
When the service runs, watching its process in the Task Manager (I know, its
only an approx.) show the memory usage of the process continually increasing
forever.
If I change the code to:
...
AnsiString cOp = FQuery->FieldByName("operator")->AsString.Trim();
AnsiString cUserName = "";
if(cOp.Length() > 0)
cUserName = FQuery->FieldByName("tsr")->AsString.Trim();
...
Then Task Manager show the memory usage as constant and never increasing.
The code produces the expected results either way...
Can anyone else reproduce this? If this is an issue, I likely have large
memory leaks in just about every piece of code I've ever written...
Mark
mguerrieri@cmsgrp.com
Re: Ternary operator memory leak
Author: Michael Kelly
Date: Thu, 03 Apr 2003 22:52
Date: Thu, 03 Apr 2003 22:52
24 lines
1757 bytes
1757 bytes
On Thu, 3 Apr 2003 03:40:31 -0500, "Mark Guerrieri" <mguerrieri@cmsgrp.com>
wrote:
>When the service runs, watching its process in the Task Manager (I know, its
>only an approx.) show the memory usage of the process continually increasing
>forever.
How about setting the condition so that it is always false
and see if you still have a leak? Seems like if the term
that should be evaluated if the condition is true, got
evaluated every time through the loop, then you would
have an allocation that's discarded(leak.) I dunno' why
this would happen though.
See the Bug++ column of Windows Developer Magazine
for March 2003 for some stuff about the ?: operator evaluating
the right hand side if there's a type mismatch. Seems bizarre
but who knows?
--
"I don't want to belong to any club that would have me as a member."
-- Groucho Marx
Re: Ternary operator memory leak
Author: Mark Guerrieri
Date: Fri, 04 Apr 2003 05:24
Date: Fri, 04 Apr 2003 05:24
45 lines
2802 bytes
2802 bytes
> How about setting the condition so that it is always false
> and see if you still have a leak? Seems like if the term
> that should be evaluated if the condition is true, got
> evaluated every time through the loop, then you would
> have an allocation that's discarded(leak.) I dunno' why
> this would happen though.
Ok, I did some more testing. Here is a simple way to reproduce the problem.
Create a project with a single form, drop a TTimer on it, set the interval
to 100 and add this code to the OnTimer event:
for(int i=0; i<100; i++)
{
AnsiString cTest = (true) ? AnsiString("Pass").Trim() :
AnsiString("Fail").Trim();
}
Run it and watch your memory bleed away. Here are all the possible
permutations:
AnsiString cTest = (true) ? AnsiString("Pass").Trim() :
AnsiString("Fail").Trim(); = LEAK
AnsiString cTest = (false) ? AnsiString("Pass").Trim() :
AnsiString("Fail").Trim(); = LEAK
AnsiString cTest = (true) ? AnsiString("Pass") : AnsiString("Fail"); = NO
LEAK
AnsiString cTest = (false) ? AnsiString("Pass") : AnsiString("Fail"); = NO
LEAK
AnsiString cTest = (true) ? AnsiString("Pass").Trim() : AnsiString("Fail");
= NO LEAK
AnsiString cTest = (false) ? AnsiString("Pass").Trim() : AnsiString("Fail");
= LEAK
AnsiString cTest = (true) ? AnsiString("Pass") : AnsiString("Fail").Trim();
= NO LEAK
AnsiString cTest = (false) ? AnsiString("Pass") : AnsiString("Fail").Trim();
= LEAK
So it seems that whenever the evaluated expression does a Trim(), memory is
leaked. Why would this be? Someone tell me I'm crazy and they can also
reproduce this so I can post it to QualityCentral.
Mark
Re: Ternary operator memory leak
Author: Michael Kelly
Date: Sat, 05 Apr 2003 00:15
Date: Sat, 05 Apr 2003 00:15
23 lines
1859 bytes
1859 bytes
On Fri, 4 Apr 2003 00:24:44 -0500, "Mark Guerrieri" <mguerrieri@cmsgrp.com>
wrote:
>So it seems that whenever the evaluated expression does a Trim(), memory is
>leaked. Why would this be?
I don't have BCB so I can't look at the source to see what
Trim does that may leak, but what I don't get is why it
would only happen with ?: operator? Other than the fact
that a temporary is created. My thought was that maybe
the expression that wasn't supposed to get evaluated
was being run anyway, creating a temporary and thereby
a dangling pointer or reference.
Maybe someone with BCB can narrow it down.
If you ever find out what's up I hope you post
what you learn.
--
"I don't want to belong to any club that would have me as a member."
-- Groucho Marx
Re: Ternary operator memory leak
Author: Remy Lebeau (TeamB)
Date: Sat, 05 Apr 2003 01:10
Date: Sat, 05 Apr 2003 01:10
17 lines
1606 bytes
1606 bytes
"Michael Kelly" <mkelly00@netzero.net> wrote in message news:oq7s8vknteieb3rhtt4hl642bk1146rfj2@4ax.com... > I don't have BCB so I can't look at the source > to see what Trim does that may leak It doesn't do anything to that effect. > but what I don't get is why it would only happen with ?: operator? I seem to recall awhile back discussions about a compiler bug in the ternary operator itself in regards to AnsiString destructors not getting called correctly. I may be wrong, though. Gambit
Re: Ternary operator memory leak
Author: Michael Kelly
Date: Sun, 06 Apr 2003 03:38
Date: Sun, 06 Apr 2003 03:38
14 lines
1454 bytes
1454 bytes
On Fri, 4 Apr 2003 17:10:22 -0800, "Remy Lebeau \(TeamB\)" <gambit47@yahoo.com>
wrote:
>I seem to recall awhile back discussions about a compiler bug in the ternary
>operator itself in regards to AnsiString destructors not getting called
>correctly. I may be wrong, though.
Thanks for the info.
--
"I don't want to belong to any club that would have me as a member."
-- Groucho Marx
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