Smart pointers question

Started by
35 comments, last by Hodgman 12 years ago
Hi, SiCrane.

I wouldn't recommend that tactic you're explaining. Constructed objects shouldn't have need to be passed to the shared_ptr constructor.

That's a big no-no right there.

20+ years of programming experience.

- Mikey
Advertisement
What on earth are you talking about? The entire point of shared_ptr is to manage a fully constructed object. You give it a valid object and it calls cleanup when all the references to it are gone. Have you even ever looked at the shared_ptr documentation? This is the first line of code in the Best Practices section: [color=#000000]shared_ptr<T> p(new Y); Notice how the object passed to the shared_ptr is fully constructed.
Hi again, SiCrane.

No, I have never researched the shared_ptr's documentation. I learned about pointers through my establishment of my first Assembly-generated game engine back many years ago. I started as a low-level programmer, and have been programming longer than you've probably been legal. I can give you at least 100 better solutions to your case.

However, given the many complex issues that may arise here, I think we should push this aside rather than debate.

- Mikey

No, I have never researched the shared_ptr's documentation.
Have a look at it -- as SiCrane mentioned, the exact case you're recommending against is the very first example of correct and intended usage.

Perhaps you could explain why you think it is a bad idea? Personally, I'd rather not "push this aside" given you've yet to explain why you're recommending against standard practice.

- Jason Astle-Adams

Hello again, jbadams.

Let's drop this before it becomes a bigger debate, please.

You may check out my external blogs from my homepage regarding reasons, tutorials and articles on why I don't recommend what the other poster offered.

Thank you.

20+ years of programming experience.

- Mikey
Perhaps you could at least link us to one example of an argument against the documented best practices?

It's kind of poor form to make a strong assertion in direct contradiction to the collective wisdom of thousands of programmers and then refuse to back up your opinion. Nobody's necessarily saying you're wrong, or that you shouldn't advocate what you're advocating. We just want to know your reasons.

At least do us the courtesy of expounding a bit. Who knows, maybe we can learn something.


As it stands, your repeated "20+ years of experience" quips and refusal to support your statements leaves a rather poor taste in my mouth, personally.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Are you really typing "20+ years of experience" as a manual post signature, coupled with giving very dubious advice?

Supertroll?
Hi, return0.

No, I'm not a "supertroll".

I'm sorry I've came across as dubious to you, but all my points will get across once my blog goes up soon.

Sorry for any inconvenience, and good luck to all your smarty pointers! :)

And it's not a manual post if it's copy/pasted. ;D

- Mikey
Alright, let me sum this up. Someone is giving dubious advice, especially in this thread but also in a few others. That does not even count the thread full of weird they created themselves [1]. The suggested 'advice' goes against the documented best practice of a well-known, widely-used library (aka Boost aka What-would-have-been-in-the-standard-library-in-a-decent-modern-language). While claiming there are "100 better solutions" they are unwilling to name and/or describe even one, be it in this thread or a newly created one. Be it here or in other places the 'advice' is never explained, but all will be made clear once that person's site with their blog goes up. The site already has a dedicated name [2] but that name is (as of my whois queries just now) not even yet registered, making it perhaps unwise to throw the name around. In the absence of any verifiable facts the person also likes to claim "20+ years of programming experience" repeatedly all over the place. Did I miss anything?

At this point my troll radar is simply off the scale. Not even a good one because it's just too much of a caricature of a cliché arrogant but clueless 'developer'. Of course there is also the possibility it's simply a medicamentation issue. A younger me might have still believed I might against all odds learn something new and interesting from this but the current me has seen something very much like this too often to actually consider that.

[1] http://www.gamedev.net/topic/622666-can-somebody-here-help-me-with-directx/page__p__4926603#entry4926603
[2] http://www.gamedev.net/topic/622666-can-somebody-here-help-me-with-directx/page__view__findpost__p__4927772
The requirement that shared_ptr has on the pointee is that for the one-argument ctor, the object pointed to shall be sourced from plain new, and as such should be destroyable with plain delete.

For the ctor that accepts a pointer and a deleter, the only requirement is that the expression d(p) should be well-formed. This doesn't however mean that you can use the memory as an object unless you've explicitly constructed an object in it via placement new or some other method.

While you can form a shared_ptr to uninitialized memory with a custom deleter, it's very counter-productive as you will not be able to use it as an object of that type and will most definitely invoke UB if you ever touch it.

To make it is hell. To fail is divine.

This topic is closed to new replies.

Advertisement