Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Exception causes memory leak (throw/catch)


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 6 - Last Post: Dec 9, 2016 12:25 PM Last Post By: David Millington
Markus Forster

Posts: 3
Registered: 4/21/14
Exception causes memory leak (throw/catch)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 30, 2016 5:34 AM
Hello,

I discovered a problem (tested with CBuilder XE5 / XE10.1 Berlin):
Throwing/catching Exceptions causes a memory leak.
Here is some example code:

while (1)
{
try
{
throw Exception("My Exception");
}
catch (Exception& E)
{
Label1->Caption = E.Message;
}
}

(Source: http://www.devsuperpage.com/search/Articles.aspx?G=2&ArtID=83153)

This causes after some time an Out Of Memory crash (32 bit executable consumes about 1.9 GB).

The posted link says, that the leak was not in CBuilder 6, but also in XE2 and XE3.
I just did tests with CBuilder 6 and I can confirm that the the above code does not create leaks in CBuilder 6.
I checked the declaration of Exception in SysUtils.hpp (CBuilder 6 / XE5), and they are quite different.
It seems that it used AnsiString in CBuilder 6, in later versions UnicodeString.
Also in CBuilder 6, the destructor is different:
inline __fastcall virtual ~Exception(void) { }
XE5:
__fastcall virtual ~Exception(void);

Is there any workaround possible?
Alex Belo

Posts: 626
Registered: 10/8/06
Re: Exception causes memory leak (throw/catch)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 1, 2016 7:06 AM   in response to: Markus Forster in response to: Markus Forster
Markus Forster wrote:

Throwing/catching Exceptions causes a memory leak.

What? Again??

Is there any workaround possible?

1) No, file QP report an wait for hotfix/update. But histiry shows that
"classic" bcc32 compiler is incurable.

2) Use another compiler (Clang-based I believe). Or problem is in
bcc32c this time ?..

--
Alex
Markus Forster

Posts: 3
Registered: 4/21/14
Re: Exception causes memory leak (throw/catch)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 6, 2016 4:04 AM   in response to: Alex Belo in response to: Alex Belo
Alex Belo wrote:
Markus Forster wrote:

Throwing/catching Exceptions causes a memory leak.

What? Again??

Is there any workaround possible?

1) No, file QP report an wait for hotfix/update. But histiry shows that
"classic" bcc32 compiler is incurable.

2) Use another compiler (Clang-based I believe). Or problem is in
bcc32c this time ?..

--
Alex

I just replaced the bcc32.exe of CBuilder XE5 (6.70.4983.33517) with the bcc32.exe of CBuilder 6 (5.6.4.0),
but the leaks stay the same. After 6321890 iterations I get an "Access violation at address 0CD19CA4 in module 'CG32.DLL'. Write of address 00000020."
I had CodeGuard activated, but it reported no leaks. Memory usage by my test program at this point: 1936648 KB.
David Millington

Posts: 20
Registered: 7/9/16
Re: Exception causes memory leak (throw/catch)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 7, 2016 4:27 AM   in response to: Alex Belo in response to: Alex Belo
1) No, file QP report an wait for hotfix/update. But histiry shows that
"classic" bcc32 compiler is incurable.

2) Use another compiler (Clang-based I believe). Or problem is in
bcc32c this time ?..

Please do file a QP, although we have several QPs already about memory leaks with exceptions in the classic compiler, which we're looking into. Adding test cases and clear notes about the version (eg Berlin vs XE2, but also which compiler: bcc32, bcc32c, bcc64) is useful.

Thanks!
Alex Belo

Posts: 626
Registered: 10/8/06
Re: Exception causes memory leak (throw/catch)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 7, 2016 7:20 AM   in response to: David Millington in response to: David Millington
David Millington wrote:

Adding test cases <...> is useful.

AFAIUI such test cases are already in EMBT everyday tests (or they must
be there) because such problems persist more than decade (since the age
of CodeGear and David Dean C++ QA Engineer); memory leaks in exceptions
can be fixed in one version and suddenly appear in next release.

--
Alex
David Millington

Posts: 20
Registered: 7/9/16
Re: Exception causes memory leak (throw/catch)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 9, 2016 12:25 PM   in response to: Alex Belo in response to: Alex Belo
Alex Belo wrote:
memory leaks in exceptions
can be fixed in one version and suddenly appear in next release.

We have extensive test cases around exceptions, but perhaps we need to expand our tests to focus on this area. But either way, adding test cases illustrating a bug to a bug report always helps us resolve the bug. It doesn't have to be a formal unit test! Just a project or code attached to the bug saying "Do X, observe Y".
Asger Joergensen

Posts: 370
Registered: 11/18/08
Re: Exception causes memory leak (throw/catch)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 2, 2016 3:21 PM   in response to: Markus Forster in response to: Markus Forster
The code does not produce leak in CB2009 either.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02