Article View: comp.lang.c++.leda
Article #17problem with leda's string and exception
From: Bruno Conductier
Date: Thu, 03 Aug 1995 07:18
Date: Thu, 03 Aug 1995 07:18
183 lines
5256 bytes
5256 bytes
hello, i novice with leda, not an C++ expert, and
got a problem with leda's string
the following is an example of string used in
conjonction with C++ exceptions.
the problem may be dwell in a bad comportment of string call
by value or exception treatment in CC.
a friend of mine trace the internal LEDA's string reference
counter and note there is a mismatch between the gestion
of this counter and the real constructor/destructor cycle call
NB: the code provide here is an small scenario build from nothing.
the code is simply duplicate for conditionnal compilation.
the main catch an error and simply display the string return
and the gravity determine by a virtual function depending on
the type of the error.
THANKS IN ADVANCE FOR ANY SUGGESTION ...
----------------------------------------
uname -a
SunOS happy 4.1.3_U1 1 sun4m
CC -> C++ 4.0
cat pbLeda.cc
//=============================================================================
=====
#include <iostream.h>
#include <LEDA/basic.h>
//=============================================================================
=====
// the first version of the exception constructor use
// a call by value
//=============================================================================
=====
#ifdef version1
class errorReport {
public:
errorReport(string text); // call by value
void diagnostic();
virtual string severity() = 0;
private:
const string text_;
errorReport(const errorReport&); // not defined to avoid unlucky copy
errorReport& operator=(const errorReport&); // not defined to avoid unlucky
recopy
};
errorReport::errorReport(string text) // call by value
: text_(text)
{
};
void errorReport::diagnostic() {
cout << "Err " << severity() << ": " << text_ << endl; // output
// call to virtual
function
}; // and print value
class errorFatal : public errorReport {
public:
errorFatal(string text); // call by value
virtual string severity();
private:
errorFatal(const errorFatal&); // not defined to avoid unlucky copy
errorFatal& operator=(const errorFatal&); // not defined to avoid unlucky
recopy
};
errorFatal::errorFatal(string text) // call by value
: errorReport(text)
{
};
string errorFatal::severity() { // implementation of pure virtual
function
return "fatal";
};
int main() {
try {
throw errorFatal("pas glop");
}
catch ( errorReport& anError) {
anError.diagnostic();
};
return( 0 );
};
#endif
//=============================================================================
=====
// the second version of the exception constructor use
// a call by reference
//=============================================================================
=====
#ifdef version2
class errorReport {
public:
errorReport(const string& text); // by reference
void diagnostic();
virtual string severity() = 0;
private:
const string text_;
errorReport(const errorReport&);
};
errorReport::errorReport(const string& text) // by reference
: text_(text)
{
};
void errorReport::diagnostic() {
cout << "Err " << severity() << ": " << text_ << endl;
};
class errorFatal : public errorReport {
public:
errorFatal(const string& text); // by reference
virtual string severity();
private:
errorFatal(const errorFatal&);
};
errorFatal::errorFatal(const string& text) // by reference
: errorReport(text)
{
};
string errorFatal::severity() {
return "fatal";
};
int main() {
try {
throw errorFatal("pas glop");
}
catch ( errorReport& anError) {
anError.diagnostic();
};
return( 0 );
};
#endif
//=============================================================================
=====
CC -g \
-Dversion1 \
-o pbLeda \
-I/u/lynus/0/secoia/local/include \
-L/u/lynus/0/secoia/local/lib/CC-lib \
-lL \
-lg \
pbLeda.cc
====================>
====================>
====================> THE PROBLEM IS HERE
====================>
====================>
bad try : the correct result should be => Err fatal: pas glop
like in version2. |
pbLeda |
Err fatal: fatal <--------------------------------------+
CC -g \
-Dversion2 \
-o pbLeda \
-I/u/lynus/0/secoia/local/include \
-L/u/lynus/0/secoia/local/lib/CC-lib \
-lL \
-lg \
pbLeda.cc
=====>correct try
pbLeda
Err fatal: pas glop
Technical Details
Message-ID:
<3vpt87$9rh@news-sop.inria.fr>Path:
arc1-ca.pugleaf.net!woodstock.inria.fr!cbrunoShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!woodstock.inria.fr!cbruno From: cbruno@woodstock.inria.fr (Bruno Conductier) Newsgroups: comp.lang.c++.leda Subject: problem with leda's string and exception Date: 3 Aug 1995 07:18:31 GMT Organization: INRIA/CERMICS Sophia-Antipolis Distribution: world Message-ID: <3vpt87$9rh@news-sop.inria.fr> Reply-To: cbruno@lynus.inria.fr NNTP-Posting-Host: woodstock.inria.fr Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit