Object validation class dying from seg faults

B

Bedevere13

I made the following class in order to validate my objects:

class Validation
{
public:
inline bool bIsValid(void) const { return (this && m_uiValidation ==
0x4321);}
inline void Verify(void) const {if (!bIsValid()) throw
std::string("Accessing invalid Object"); }
Validation(void) {Validate();}
virtual ~Validation(void) {Invalidate();}

protected:
inline void Validate(void) {m_uiValidation = 0x4321;}
inline void Invalidate(void) {m_uiValidation = 0;}

private:
unsigned int m_uiValidation;
};

Before I use an object, I will check bIsValid(), which is good for
catching NULL pointers, but I have gotten some seg faults from the
bIsValid() code when there is a bad pointer. Does anyone know how to
not die in that function if the this pointer is bad and to just return
false so the calling code can deal with it?
 
I

int2str

I made the following class in order to validate my objects:

class Validation
{
public:
inline bool bIsValid(void) const { return (this && m_uiValidation ==
0x4321);}

"void" for empty argument lists is a "C-ism" and makes the eyes of
some of the most knowledgeable people on this group bleed. ;)

But much more importantly, the answer you're looking for is "No" - I
believe. Everytime you access a member variable in this fashion,
you're causing any possible pointer to the object to be de-referenced
to access the member. If the pointer is not valid and outside the
process space of your application, you'll get a segmentation fault (or
platform equivalent).

There may be platform specific constructs to avoid the segfault, or
catch it, but IMHO there's no standard way of doing it.

A much better choice would be defensive programming practices that
avoid such a possible scenario as much as possible.
inline void Verify(void) const {if (!bIsValid()) throw
std::string("Accessing invalid Object"); }
Validation(void) {Validate();}
virtual ~Validation(void) {Invalidate();}

Why not just put the relevant assignments directly into the
constructor destructor?
protected:
inline void Validate(void) {m_uiValidation = 0x4321;}
inline void Invalidate(void) {m_uiValidation = 0;}

private:
unsigned int m_uiValidation;

};

Before I use an object, I will check bIsValid(), which is good for
catching NULL pointers, but I have gotten some seg faults from the
bIsValid() code when there is a bad pointer. Does anyone know how to
not die in that function if the this pointer is bad and to just return
false so the calling code can deal with it?

Cheers,
Andre
 
J

James Kanze

I made the following class in order to validate my objects:
class Validation
{
public:
inline bool bIsValid(void) const { return (this && m_uiValidation ==
0x4321);}
inline void Verify(void) const {if (!bIsValid()) throw
std::string("Accessing invalid Object"); }
Validation(void) {Validate();}
virtual ~Validation(void) {Invalidate();}
protected:
inline void Validate(void) {m_uiValidation = 0x4321;}
inline void Invalidate(void) {m_uiValidation = 0;}
private:
unsigned int m_uiValidation;
};
Before I use an object, I will check bIsValid(), which is good for
catching NULL pointers, but I have gotten some seg faults from the
bIsValid() code when there is a bad pointer. Does anyone know how to
not die in that function if the this pointer is bad and to just return
false so the calling code can deal with it?

No. Any time that bIsValid would return false, you've already
had undefined behavior beforehand, so anything could happen.

Also, I'm not sure of the context you're running in, but
generally, if you have a programming error, crashing is the best
thing you can do. That ensures that the error doesn't go
unnoticed.
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top