Will this leak ? (question on throw statement)

B

Bit byte

I have a simplistic exception class like so:

class CommException {
public:
CommException(const char *err){ strncpy(msg,err,ERR_MSG_LEN); }
virtual ~CommException(){;}
void erase(void) { memset(msg,'\\0',ERR_MSG_LEN); }
const char* report(void) { return msg ; }

private:
CommException( const CommException&);
CommException& operator= (const CommException&) ;

char msg[ERR_MSG_LEN+1] ;
};


When I use this kinda statement:

throw CommException("Blah, blah, blan") ; //compiler barfs here

Unless I do this:

throw new CommException("Blah, blah, blah") ;


In my catch statement, I have this:

catch (const CommException& e) {
//do something with e
//recvd const *ref*, so I cant 'delete' (maybe this is a an example of
a time that one NEEDS to receive an exception by a pointer?
}

Since I am using the *new* keyword (I still don't know why my compiler
insists on this), I assume I am alloc'ing memory from the heap rather
than the stack - so do I need to free this in my catch statement?
 
B

Bit byte

Bit said:
I have a simplistic exception class like so:

class CommException {
public:
CommException(const char *err){ strncpy(msg,err,ERR_MSG_LEN); }
virtual ~CommException(){;}
void erase(void) { memset(msg,'\\0',ERR_MSG_LEN); }
const char* report(void) { return msg ; }

private:
CommException( const CommException&);
CommException& operator= (const CommException&) ;

char msg[ERR_MSG_LEN+1] ;
};


When I use this kinda statement:

throw CommException("Blah, blah, blan") ; //compiler barfs here

Unless I do this:

throw new CommException("Blah, blah, blah") ;


In my catch statement, I have this:

catch (const CommException& e) {
//do something with e
//recvd const *ref*, so I cant 'delete' (maybe this is a an
example of a time that one NEEDS to receive an exception by a pointer?
}

Since I am using the *new* keyword (I still don't know why my compiler
insists on this), I assume I am alloc'ing memory from the heap rather
than the stack - so do I need to free this in my catch statement?

Just been doing some further reading (thanks to parashift), and I
strongly suspect my compilers insistence on the new keyword is related
to my hidden copy constructor in the CommException class ... (please
correct me if I'm headed down the wrong path though)
 
P

peter koch

Bit byte skrev:
I have a simplistic exception class like so:

class CommException {
public:
CommException(const char *err){ strncpy(msg,err,ERR_MSG_LEN); }
virtual ~CommException(){;}
void erase(void) { memset(msg,'\\0',ERR_MSG_LEN); }
const char* report(void) { return msg ; }

private:
CommException( const CommException&);
CommException& operator= (const CommException&) ;

Why are these private?
char msg[ERR_MSG_LEN+1] ;
};


When I use this kinda statement:

throw CommException("Blah, blah, blan") ; //compiler barfs here

Why does it barf? An exception needs to be copyable. I believe this is
what the barf says.
Unless I do this:

throw new CommException("Blah, blah, blah") ;

This is another expression as the first one - and needs another catch.
In my catch statement, I have this:

catch (const CommException& e) {
//do something with e
//recvd const *ref*, so I cant 'delete' (maybe this is a an example of
a time that one NEEDS to receive an exception by a pointer?
}
Will never catch the "new" throw expression
Since I am using the *new* keyword (I still don't know why my compiler
insists on this), I assume I am alloc'ing memory from the heap rather
than the stack - so do I need to free this in my catch statement?

You would - and you would have to catch something else. But fix the
basic problem instead.

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top