Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#Actualfastcall22

Posted 03 October 2013 - 02:04 PM

std::shared_ptr<PeerData> spPeer((PeerData*)netevent.peer->data);


When you construct a shared pointer from a raw pointer, the shared pointer assumes ownership of the pointer. See the sample code below:
#include <memory>

int main(int,char*[]) {
    typedef std::shared_ptr<int> SharedInt;
    void* ptr;
    
    SharedInt a(new int);
    /*1*/ {
        ptr = &(*a);
    }
    
    /*2*/ {
        SharedInt b( (int*)ptr );
        *b = 5;
    } // b is destroyed, ptr is deleted, and a is dangling
} // Crash once a is destroyed

#4fastcall22

Posted 03 October 2013 - 02:03 PM

std::shared_ptr<PeerData> spPeer((PeerData*)netevent.peer->data);


Don't do that. See the sample code below:
#include <memory>

int main(int,char*[]) {
    typedef std::shared_ptr<int> SharedInt;
    void* ptr;
    
    SharedInt a(new int);
    /*1*/ {
        ptr = &(*a);
    }
    
    /*2*/ {
        SharedInt b( (int*)ptr );
        *b = 5;
    } // b is destroyed, ptr is deleted, and a is dangling
} // Crash

#3fastcall22

Posted 03 October 2013 - 02:02 PM

std::shared_ptr<PeerData> spPeer((PeerData*)netevent.peer->data);


Don't do that:
#include <memory>

int main(int,char*[]) {
    typedef std::shared_ptr<int> SharedInt;
    void* ptr;
    
    SharedInt a(new int);
    /*1*/ {
        ptr = &(*a);
    }
    
    /*2*/ {
        SharedInt b( (int*)ptr );
        *b = 5;
    } // b is destroyed, ptr is deleted, and a is dangling
} // Crash

#2fastcall22

Posted 03 October 2013 - 02:02 PM

std::shared_ptr<PeerData> spPeer((PeerData*)netevent.peer->data);
[/quote]

Don't do that:
#include <memory>

int main(int,char*[]) {
    typedef std::shared_ptr<int> SharedInt;
    void* ptr;
    
    SharedInt a(new int);
    /*1*/ {
        ptr = &(*a);
    }
    
    /*2*/ {
        SharedInt b( (int*)ptr );
        *b = 5;
    } // b is destroyed, ptr is deleted, and a is dangling
} // Crash
[/quote]


#1fastcall22

Posted 03 October 2013 - 02:02 PM

[/quote]

std::shared_ptr<PeerData> spPeer((PeerData*)netevent.peer->data);
[/quote]

Don't do that:
#include <memory>

int main(int,char*[]) {
    typedef std::shared_ptr<int> SharedInt;
    void* ptr;
    
    SharedInt a(new int);
    /*1*/ {
        ptr = &(*a);
    }
    
    /*2*/ {
        SharedInt b( (int*)ptr );
        *b = 5;
    } // b is destroyed, ptr is deleted, and a is dangling
} // Crash


PARTNERS