Connecting Tech Pros Worldwide Forums | Help | Site Map

Will this leak ? (question on throw statement)

Bit byte
Guest
 
Posts: n/a
#1: Apr 22 '06
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?



Bit byte
Guest
 
Posts: n/a
#2: Apr 22 '06

re: Will this leak ? (question on throw statement)




Bit byte wrote:
[color=blue]
> 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?
>
>[/color]

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)

peter koch
Guest
 
Posts: n/a
#3: Apr 22 '06

re: Will this leak ? (question on throw statement)



Bit byte skrev:
[color=blue]
> 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&) ;[/color]

Why are these private?[color=blue]
>
> char msg[ERR_MSG_LEN+1] ;
> };
>
>
> When I use this kinda statement:
>
> throw CommException("Blah, blah, blan") ; //compiler barfs here[/color]

Why does it barf? An exception needs to be copyable. I believe this is
what the barf says.[color=blue]
>
> Unless I do this:
>
> throw new CommException("Blah, blah, blah") ;[/color]

This is another expression as the first one - and needs another catch.[color=blue]
>
>
> 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?
> }[/color]
Will never catch the "new" throw expression[color=blue]
>
> 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?[/color]

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

/Peter

Closed Thread