# shared_ptr == NULL???

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

## 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 on other sites

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

// calling function

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

##### 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 functionshared_object<Object> o = LoadObject("fred");if (!o){    cout << "Fred Not Loaded";}

##### 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 on other sites
Quote:
 Original post by SneftelIt'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 on other sites
Quote:
 Original post by MadhedPay 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 on other sites
Quote:
 Original post by SneftelUhhhh....... 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 on other sites
Quote:
 Original post by SneftelUhhhh....... since when?

I am wondering the same thing.

##### 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 on other sites
Quote:
 Original post by SneftelIt'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 on other sites
Quote:
Original post by Agony
Quote:
 Original post by SneftelIt'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?

Yes, you are absolutely correct. Independently creating multiple shared_ptr from the same raw pointer (NULL pointer excepted) will cause things to break down, since they keep independent reference counts.

##### Share on other sites
Quote:
Original post by Agony
Quote:
 Original post by SneftelIt'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.

Yeah, that's what I originally thought and cause me not to look at the solution Sneftel suggested originally, please can someone confirm this one way or another before i write too much more code?

^^^ That was me

##### Share on other sites
Quote:
 Original post by FrunyYes, you are absolutely correct. Independently creating multiple shared_ptr from the same raw pointer (NULL pointer excepted) will cause things to break down, since they keep independent reference counts.

OK, so this takes me all the way back to my original question...how do I/can I return a NULL shared_ptr?

Edit:
is myptr.reset() good for setting a null pointer

and (myptr.get() == NULL) good for testing?

##### Share on other sites
Can't you just return a new shared_ptr object and then check the get() method for 0?

shared_ptr<object> function(){shared_ptr<object> mySharedPtr;return mySharedPtr}if(function().get() == 0){//NULL SharedPtr}

I THINK that works...

##### Share on other sites
Quote:
 Original post by xstreme2000OK, so this takes me all the way back to my original question...how do I/can I return a NULL shared_ptr?Edit:is myptr.reset() good for setting a null pointer

Yes. Though given your code, you don't even need it, since the else branch is only taken if obj = somehowLoadObject() evaluates to false, that is, if obj holds a null pointer.

Quote:
 and (myptr.get() == NULL) good for testing?

Or just if (!myptr).

##### Share on other sites
Quote:
Original post by Fruny
Quote:
 Original post by xstreme2000OK, so this takes me all the way back to my original question...how do I/can I return a NULL shared_ptr?Edit:is myptr.reset() good for setting a null pointer

Yes. Though given your code, you don't even need it, since the else branch is only taken if obj = somehowLoadObject() evaluates to false, that is, if obj holds a null pointer.

Excellent, I understood it would not have been needed in that case although that was a very basic example and the actual code has lots extra going on.

Quote:
Original post by Fruny
Quote:
 and (myptr.get() == NULL) good for testing?

Or just if (!myptr).

Greate, that's exactly what I need!