Jump to content
  • Advertisement
Sign in to follow this  
xstreme2000

shared_ptr == NULL???

This topic is 4523 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm sure this is a very simple question but I'm not overly familiar with the STL/Boost. If I've got a function which returns a smart pointer (more specifically a share_ptr) and a want set/check it for a NULL value how do I do it... e.g. (not using shared_ptr)
Object *LoadObject(string name)
{
    Object *obj;

    if (obj = somehowLoadObject())
    {
         return obj;
    }
    else
    {
         return NULL;
    }
}

// Then in main()...
Object *o;
if ((o = LoadObject("fred.obj")) == NULL) {
    cerr << "Object not loaded." << endl;
}
my guess this far
shared_ptr<Object> LoadObject(string name)
{
    shared_ptr<Object> obj;

    if (obj = somehowLoadObject())
    {
         return obj;
    }
    else
    {
         obj.reset(); // NULL pointer equivelent???
         return obj;
    }
}

// Then in main()...
shared_ptr<Object> o;
if ( /* WHAT TEST GOES HERE? */ ) {
    cerr << "Object not loaded." << endl;
}

Share this post


Link to post
Share on other sites
Advertisement

shared_ptr<Object> LoadObject(string name)
{
// this might have to be return shared_ptr<Object>(somhowLoadObject());
return somehowLoadObject();
}

// calling function

shared_object<Object> o = LoadObject("fred");
if (!o)
{ cout << "Fred Not Loaded";
}

Share this post


Link to post
Share on other sites
hmm, is that really the only way to do it? Reason this concerns me is because my load function is storing the pointer somewhere else too and this I assume would end up meaning I'd have to shared_ptrs to the same block of memory which I would assume would mean it would get deleted twice (am I wrong)?



shared_ptr<Object> StashedObject;

shared_ptr<Object> LoadObject(string name)
{
Object *obj = somehowLoadObject();
StashedObject.reset(obj);
return obj;
}

// calling function

shared_object<Object> o = LoadObject("fred");
if (!o)
{
cout << "Fred Not Loaded";
}

Share this post


Link to post
Share on other sites
It's perfectly fine to have two shared_ptrs to the same object. That's why they call them SHARED_ptr.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
It's perfectly fine to have two shared_ptrs to the same object. That's why they call them SHARED_ptr.


Oh? wicked, I always assumed you had to have more than one pointing to the same object by assigning 1 to an other, if that's not the case then great :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Madhed
Pay attention though!

AFAIK

"if (obj = somehowLoadObject())"

does NOT equal "if ((obj = somehowLoadObject()))"

(see the extra braces?)

the first will always return true while the latter will return the value of obj after the assignment operation.

Uhhhh....... since when?

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Uhhhh....... since when?


Erm, I think he might have confused with thinking about these 2 conditions

if (obj = loadobject() == NULL) doSomething();

and

if ((obj = loadobject()) == NULL) doSomething();

Share this post


Link to post
Share on other sites
Um... sorry for the confusion. I deleted my post.

I just went to VS.Net to check... you're absolutely right! O_o

Maybe I gathered that "knowledge" when I still programmed with the "good old" VC++6.0.
In VC++6.0 I always had to do that and now I still do it...

sorry again

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
It's perfectly fine to have two shared_ptrs to the same object. That's why they call them SHARED_ptr.

Isn't that only when the all shared_ptrs except the first one are constructed/assigned from the first shared_ptr, rather than being constructed from the raw pointer itself? In his code, it appears as though he's resetting StashedObject to reference obj, and is then returning obj, which should cause the creation of another shared_ptr. (Except I'm expecting that part to fail, if I remember correctly, because a shared_ptr must be constructed explicitly, right?) So even with an explicit constructor on the return:
shared_ptr<Object> LoadObject(string name)
{
Object *obj = somehowLoadObject();
StashedObject.reset(obj);
return shared_ptr<Object>(obj);
}
Wouldn't that create two independent sets of shared_ptrs that point to the same thing, each thinking they own that block of memory? And thus get the double-delete issue that xstreme2000 was worried about? I would think that this would be the proper way to do it:
shared_ptr<Object> LoadObject(string name)
{
Object *obj = somehowLoadObject();
StashedObject.reset(obj);
return StashedObject;
}
By creating a copy of StashedObject, you still only have one set of shared_ptrs. StashedObject is the only one that is ever constructed/reset from the raw pointer.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!