Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How do you detect and handle invalid pointers?

Reply
Thread Tools

How do you detect and handle invalid pointers?

 
 
DeMarcus
Guest
Posts: n/a
 
      11-02-2010
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
 
Reply With Quote
 
 
 
 
Stuart Golodetz
Guest
Posts: n/a
 
      11-02-2010
On 02/11/2010 08:30, DeMarcus wrote:
> 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
 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      11-02-2010
On Tue, 2010-11-02, DeMarcus wrote:
> 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

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
DeMarcus
Guest
Posts: n/a
 
      11-02-2010
On 11/02/2010 10:42 AM, Stuart Golodetz wrote:
> On 02/11/2010 08:30, DeMarcus wrote:
>> 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


Thanks, that was a good tip!

 
Reply With Quote
 
Krice
Guest
Posts: n/a
 
      11-02-2010
On 2 marras, 10:30, DeMarcus <use_my_alias_h...@hotmail.com> wrote:
> 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.
 
Reply With Quote
 
DeMarcus
Guest
Posts: n/a
 
      11-02-2010
On 11/02/2010 12:35 PM, Jorgen Grahn wrote:
> On Tue, 2010-11-02, DeMarcus wrote:
>> 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
>


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.


 
Reply With Quote
 
Balog Pal
Guest
Posts: n/a
 
      11-02-2010
"DeMarcus" <>
>>> 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.


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.

 
Reply With Quote
 
DeMarcus
Guest
Posts: n/a
 
      11-02-2010
On 11/02/2010 02:20 PM, Balog Pal wrote:
> "DeMarcus" <>
>>>> 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.

>
> 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.
>


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.

 
Reply With Quote
 
BGB / cr88192
Guest
Posts: n/a
 
      11-02-2010

"DeMarcus" <> wrote in message
news:4ccfcc35$0$23763$...
> 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...


 
Reply With Quote
 
Chris M. Thomasson
Guest
Posts: n/a
 
      11-03-2010
"Paavo Helde" <> wrote in message
news:Xns9E2597FCB483myfirstnameosapriee@216.196.10 9.131...

[...]

> To be honest, I even cannot
> imagine how I could safely access a dynamically deletable object from
> different threads using a naked pointer only.


FWIW, Proxy Garbage Collection solves the problem:

http://groups.google.com/group/comp....b1f250a096cebf

Cool thing is that the PGC can be coded in 100% pure C++0x; it's completely
portable!

:^)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Ruby 1.9.1, Threads and "[BUG] The handle is invalid." John Briggs Ruby 10 04-21-2010 08:42 PM
Detect file handle leaks sgane2001@yahoo.co.in Java 17 07-28-2009 09:04 AM
invalid character in base-64 string and invalid postback or callba kevin ASP .Net 0 01-16-2008 09:39 PM
Detect and handle Request Timeout =?Utf-8?B?QnJpYW4gUGVhcnNvbg==?= ASP .Net 0 05-04-2006 02:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57