• 11
• 9
• 10
• 9
• 10

shared_ptr == NULL???

This topic is 4343 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;

{
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;

{
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

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.