Qt-interest Archive, March 2002
QString and C++ exception
Message 1 in thread
Hi!
I have defined exception classes in this way:
class CrException {
public:
CrException() { }
virtual ~CrException() { }
virtual const QString& what() const = 0;
};
class CrException_logicError : public CrException {
QString m_what;
public:
CrException_logicError(const QString& what_arg) : m_what(what_arg) { }
virtual const QString& what() const { return m_what; }
protected:
CrException_logicError() : CrException() { }
};
class CrException_runtimeError : public CrException {
QString m_what;
public:
CrException_runtimeError(const QString& what_arg) : m_what(what_arg) { }
virtual const QString& what() const { return m_what; }
protected:
CrException_runtimeError() : CrException() { }
};
...but they did't work :( I'm using qt-2.3.2.
Each time I throw such exception the program crashes in the QShared::deref
method. Has anybody tried to raise the exception classes with QString as
member variable?
Thank you for any hints.
Jozef Kosoru
Message 2 in thread
Seg fault?
Here's my guess, lacking more code:
1. Something throws an exception.
2. It calls your class, and gets the reference to the string.
3. Your class goes out of scope.
4. It tries to use your string, which has gone out of scope with the class.
That'd be my first guess. The remedy for that is to return the string, which
would allow the copy constructor to be invoked, so it gets what it wants, and
your class can safely go out of scope.
I hope this helps.
--
[ signature omitted ]
Message 3 in thread
Hi John,
...yes, your example works :)
The problem probably is that I'm using those exeception classes
in the non-GUI thread and QString as an implicitly shared
class has a problem with this.
I tried to call 'prepend' function in the constructor like:
CrException_logicError(const QString& what_arg) : m_what(what_arg) {m
_what.prepend(QString::fromLatin1("[CrException_logicError] ")); }
to detach from the shared memory, but with no success. :-(
But anyway thank you for your answer.
Regards
Jozef
On Sunday 10 March 2002 17:34, you wrote:
> Hey Jozef,
>
> Well, I just tried a simple example:
>
> #include <stdio.h>
> #include "qstring.h"
>
> class CrException {
> public:
> CrException() { }
> virtual ~CrException() { }
> virtual const QString& what() const = 0;
> };
>
> class CrException_runtimeError : public CrException {
> QString m_what;
> public:
> CrException_runtimeError(const QString& what_arg) : m_what(what_arg) { }
> virtual const QString& what() const { return m_what; }
> protected:
> CrException_runtimeError() : CrException() { }
> };
>
> void someFunction() throw(CrException_runtimeError)
> {
> throw CrException_runtimeError("You suck");
> }
>
> int main()
> {
> try
> {
> someFunction();
> }
> catch(CrException &e)
> {
> printf("%s\n", e.what().latin1());
> }
>
> return(0);
> }
>
> Using qt 2.3.2 and it worked fine for me, admittedly my example is
> a trivial one but you might try compiling/running my little example under
> your setup - if it doesn't blow up then perhaps the problem isn't with
> using QString in an exception specifically.