Bug 5636 - gcc-3.0.3, memory leakage: function that take a string as parameter will not call local variable destructors if exception is thrown.
Summary: gcc-3.0.3, memory leakage: function that take a string as parameter will not ...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.0.3
: P3 normal
Target Milestone: ---
Assignee: Jason Merrill
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2002-02-08 12:26 UTC by hugo-hed
Modified: 2003-07-25 17:33 UTC (History)
1 user (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments
test1.ii.gz (72.25 KB, application/x-gzip )
2003-05-21 15:16 UTC, hugo-hed
Details

Note You need to log in before you can comment on or make changes to this bug.
Description hugo-hed 2002-02-08 12:26:01 UTC
gcc-3.0.3:

A function called with a string as a parameter which is interrupted by an exception will not call the destructors of its local variables. Memory leakage!

This problem does not occur with the gcc-2.95.3 compiler.

I have tried to call the function (see code in section How-To-Repeat) with an empty parameter list, an int, a bool, a vector<int> and a struct that I defined myself. It all works very well! But if the function is called with a string, it just won't do it.

Release:
3.0.3

Environment:
System: Linux l399.dsv.su.se 2.2.19-6.2.1 #1 Mon Apr 9 22:52:58 EDT 2001 i686 unknown
Architecture: i686
	
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ../gcc-3.0.3/configure --prefix=/usr/local/gcc-3.0.3

How-To-Repeat:
I have compiled the code with command
% g++ -Wall test1.cpp -o test1

Here is an sample code that will cause the bug to ..eh.. bug (please excuse my poor English):

// Non working code
#include <iostream>
#include <string>
using namespace std;

class Test{
public:
  Test()  {
    cout << "CONSTRUCTOR\n" ;
  }
  ~Test()  {
    cout << "DESTRUCTOR\n"; 
  }
};

Test funk(string) throw (int) {
  Test t;
  throw 13;
  return t;
}

int main() {
  try {
    Test t2 = funk("tjena");
    cout << "No exception caught. " << endl;
  } catch (int error) {
    cout << "Exception caught: error = " << error << endl;
  }
}
_________________________
The code above gives me the output:
% ./test1 
CONSTRUCTOR
Exception caught: error = 13
</end output> 
which shows that the destructor for the Test ocject is never called.

Below some similar, but working code:
____________________________________
// Working code
#include <iostream>
#include <string>
using namespace std;

class Test{
public:
  Test() {
    cout << "CONSTRUCTOR. \n";
  }
  ~Test() {
    cout << "DESTRUCTOR. \n";    
  }
};

Test funk() throw (int) { 
  Test t;
  throw 13;
  return t;
}

int main() {
  try {
    Test t2 = funk();
    cout << "No exception caught. " << endl;
  } catch (int error) {
    cout << "Exception caught: error = " << error << endl;
  }
}
____________________________________
This code will correctly give me the output:
% ./test2 
CONSTRUCTOR. 
DESTRUCTOR. 
Exception caught: error = 13
</end output>
and yes, a destructor has been called. As mentioned in the Description section, the fuction will also work well called with an int, a bool, a struct and an empty parameter list.
Comment 1 hugo-hed 2002-02-08 12:26:01 UTC
Fix:
I haven't really - really - explored the problems/possibilities of this bug/bug-fix. Is a string parameter the only kind of parameter to cause the problem?

If so: In each function called on with a string, catch every possible exception, explicitly destoy (call the destructors of) the local variables, and finally re-throw the exception.
Comment 2 Jason Merrill 2002-04-03 14:52:36 UTC
Responsible-Changed-From-To: unassigned->jason
Responsible-Changed-Why: mine
Comment 3 Jason Merrill 2002-04-03 17:38:17 UTC
State-Changed-From-To: open->closed
State-Changed-Why: Thanks for the report!  Fixed for 3.1.