How do you detect and handle invalid pointers?

D

DeMarcus

Hi,

How do you detect and handle invalid pointers to avoid segmentation fault?

I have a callback scenario that looks like this. (using C++0x)

#include <functional>
#include <string>
#include <iostream>
#include <vector>

class SomeObject
{
public:
void fnc( const std::string& text )
{
std::cout << text << std::endl;
}
};

int main()
{
std::vector<std::function<void()> > callbacks;
SomeObject* s = new SomeObject;

callbacks.push_back( std::bind( &SomeObject::fnc, s, "Hello" ) );
callbacks.push_back( std::bind( &SomeObject::fnc, s, "World" ) );
callbacks.push_back( std::bind( &SomeObject::fnc, s, "The End" ) );

std::vector<std::function<void()> >::iterator i = callbacks.begin();
std::vector<std::function<void()> >::iterator end = callbacks.end();
for( int n = 0; i != end; ++i, ++n )
{
(*i)(); // Run the callback.

// At some point during this loop s is deleted. Could be from
// another thread.
if( n == 1 )
delete s;
}

return 0;
}

This callback trouble is just an example, but how are invalid pointers
detected in general? Is there a smart pointer that can handle this? Like
throwing an exception or just not run the callback.


Thanks,
Daniel
 
S

Stuart Golodetz

Hi,

How do you detect and handle invalid pointers to avoid segmentation fault?

I have a callback scenario that looks like this. (using C++0x)

#include <functional>
#include <string>
#include <iostream>
#include <vector>

class SomeObject
{
public:
void fnc( const std::string& text )
{
std::cout << text << std::endl;
}
};

int main()
{
std::vector<std::function<void()> > callbacks;
SomeObject* s = new SomeObject;

callbacks.push_back( std::bind( &SomeObject::fnc, s, "Hello" ) );
callbacks.push_back( std::bind( &SomeObject::fnc, s, "World" ) );
callbacks.push_back( std::bind( &SomeObject::fnc, s, "The End" ) );

std::vector<std::function<void()> >::iterator i = callbacks.begin();
std::vector<std::function<void()> >::iterator end = callbacks.end();
for( int n = 0; i != end; ++i, ++n )
{
(*i)(); // Run the callback.

// At some point during this loop s is deleted. Could be from
// another thread.
if( n == 1 )
delete s;
}

return 0;
}

This callback trouble is just an example, but how are invalid pointers
detected in general? Is there a smart pointer that can handle this? Like
throwing an exception or just not run the callback.


Thanks,
Daniel

Well it's not a method that's necessarily suitable for every situation,
but you can sometimes deal with things like this by storing weak
pointers. When all shared pointers that point to the same thing are
reset elsewhere, that's like doing the delete above -- except that now
you can tell this has happened because locking the weak pointer will
fail. I've found it to be a useful approach at times.

HTH,
Stu
 
J

Jorgen Grahn

Hi,

How do you detect and handle invalid pointers to avoid segmentation fault?

I don't; I try to design my code so they aren't generated.

/Jorgen
 
D

DeMarcus

Well it's not a method that's necessarily suitable for every situation,
but you can sometimes deal with things like this by storing weak
pointers. When all shared pointers that point to the same thing are
reset elsewhere, that's like doing the delete above -- except that now
you can tell this has happened because locking the weak pointer will
fail. I've found it to be a useful approach at times.

HTH,
Stu

Thanks, that was a good tip!
 
K

Krice

How do you detect and handle invalid pointers to avoid segmentation fault?

I handle pointers with strict ownership rules which is most
cases reduce the possibility for invalid pointers.
 
D

DeMarcus

I don't; I try to design my code so they aren't generated.

/Jorgen

Yes, I agree, but when dealing with asynchronous calls from different
threads there is a compromise in complexity. To make simple callbacks
work from other threads it would be cumbersome in my current situation
to introduce a whole modular framework.
 
B

Balog Pal

DeMarcus said:
Indeed.

Yes, I agree, but when dealing with asynchronous calls from different
threads there is a compromise in complexity. To make simple callbacks work
from other threads it would be cumbersome in my current situation to
introduce a whole modular framework.

The wisdom goes, good work takes time, bad work takes much more time.

You want to evade sensble design and its proper implementation to supposedly
save work. You're in a huge company with that same idea. Maybe you want to
join the other side that sees the folly.

Also i would not advide to mess with threads before you learnt how to shell
out a correct thing in ST.

If you're really stuck with the provided pointer idea, use Java or some
alike language. With C/C++ you'll just blow up stuff.
 
D

DeMarcus

The wisdom goes, good work takes time, bad work takes much more time.

I know. I'll do the best of the situation.
You want to evade sensble design and its proper implementation to
supposedly save work. You're in a huge company with that same idea.
Maybe you want to join the other side that sees the folly.

Also i would not advide to mess with threads before you learnt how to
shell out a correct thing in ST.

What's ST?
If you're really stuck with the provided pointer idea, use Java or some
alike language. With C/C++ you'll just blow up stuff.

Hehe. Wish me luck. :)
 
B

BGB / cr88192

DeMarcus said:
Hi,

How do you detect and handle invalid pointers to avoid segmentation fault?

I have a callback scenario that looks like this. (using C++0x)

#include <functional>
#include <string>
#include <iostream>
#include <vector>

class SomeObject
{
public:
void fnc( const std::string& text )
{
std::cout << text << std::endl;
}
};

int main()
{
std::vector<std::function<void()> > callbacks;
SomeObject* s = new SomeObject;

callbacks.push_back( std::bind( &SomeObject::fnc, s, "Hello" ) );
callbacks.push_back( std::bind( &SomeObject::fnc, s, "World" ) );
callbacks.push_back( std::bind( &SomeObject::fnc, s, "The End" ) );

std::vector<std::function<void()> >::iterator i = callbacks.begin();
std::vector<std::function<void()> >::iterator end = callbacks.end();
for( int n = 0; i != end; ++i, ++n )
{
(*i)(); // Run the callback.

// At some point during this loop s is deleted. Could be from
// another thread.
if( n == 1 )
delete s;
}

return 0;
}

This callback trouble is just an example, but how are invalid pointers
detected in general? Is there a smart pointer that can handle this? Like
throwing an exception or just not run the callback.

apart from detecting NULL pointers, usually validation is uncommon.
usually, an entirely invalid pointer showing up somewhere expected is
uncommon apart from programmer error, and hence the crash is usually not
such a bad thing (in many cases, it will point right to the source of the
error).

better is to try to write decent code, rather than rely on cheap tricks to
try to make it work.


the segfault is itself an exception (of sorts), hence one can catch it and
use this to detect the invalid pointer.
on Linux/... there is "signal()", but it is a hassle to use (and signal does
not usually respect threads).
on Windows, one can use SEH, which is a little nicer.

there are ways to validate pointers, but they have drawbacks:
typically, they depend on a specific context, such as validating that a
pointer is into a custom-managed heap (via checking address ranges);
often, they may involve OS specific facilities;
the logic may become complex or slow for certain non-trivial cases (IOW,
where one actually tries to check where a pointer points to, rather than
"defer and see if it blows up", or "check if page is mapped", or similar);
....


how to implement any of this will not be described here for now though...
 

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

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top