How do you detect and handle invalid pointers?

Discussion in 'C++' started by DeMarcus, Nov 2, 2010.

  1. DeMarcus

    DeMarcus Guest

    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
    DeMarcus, Nov 2, 2010
    #1
    1. Advertising

  2. 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
    Stuart Golodetz, Nov 2, 2010
    #2
    1. Advertising

  3. DeMarcus

    Jorgen Grahn Guest

    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 .
    Jorgen Grahn, Nov 2, 2010
    #3
  4. DeMarcus

    DeMarcus Guest

    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!
    DeMarcus, Nov 2, 2010
    #4
  5. DeMarcus

    Krice Guest

    On 2 marras, 10:30, DeMarcus <> 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.
    Krice, Nov 2, 2010
    #5
  6. DeMarcus

    DeMarcus Guest

    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.
    DeMarcus, Nov 2, 2010
    #6
  7. DeMarcus

    Balog Pal Guest

    "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.
    Balog Pal, Nov 2, 2010
    #7
  8. DeMarcus

    DeMarcus Guest

    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. :)
    DeMarcus, Nov 2, 2010
    #8
  9. "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...
    BGB / cr88192, Nov 2, 2010
    #9
  10. "Paavo Helde" <> wrote in message
    news:Xns9E2597FCB483myfirstnameosapriee@216.196.109.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..._frm/thread/a53f24de178b419f/30b1f250a096cebf

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

    :^)
    Chris M. Thomasson, Nov 3, 2010
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    17
    Views:
    15,115
    pavel.genevski
    Jul 28, 2009
  2. =?Utf-8?B?QnJpYW4gUGVhcnNvbg==?=

    Detect and handle Request Timeout

    =?Utf-8?B?QnJpYW4gUGVhcnNvbg==?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    419
    =?Utf-8?B?QnJpYW4gUGVhcnNvbg==?=
    May 4, 2006
  3. kevin
    Replies:
    0
    Views:
    942
    kevin
    Jan 16, 2008
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    641
  5. John Briggs
    Replies:
    10
    Views:
    195
    Luis Lavena
    Apr 21, 2010
Loading...

Share This Page