Why shared ptr is bad




















There are three ways to go about solving this:. Where is the bug, then? Is it in bar , or is it in foo? Of course the bug is in bar. Without this assumption, our programs are inherently meaningless and all code is fundamentally broken.

As such, the best we can do is write code the assumption that our caller has not given us a moved-from object. This is mostly a documentation change, as there is no way to enforce that a user to never touch a moved-from object. Here is what such a documentation might look like:. We could change the socket to be copy-only , but this changes the semantics of the library. Having an implicitly-shared socket makes enforcing a protocol difficult.

You seem to be browsing with JavaScript disabled. This site contains no trackers, beacons, ads, or any other annoying elements unless you opt-in. This site uses JavaScript for its true purpose: To enhance the user experience. It means that you probably need to pass a shared pointer to a thread only if you run a detached thread. How to fix it?

It looks almost the same but this code makes only one allocation of a contiguous piece of memory used for storing both the stored object and the reference counter. The picture below shows the difference in the memory layout of shared pointers created in the two ways. Its output is empty which means the destructors are never called due to a reference cycle: ptrA points to ptrB and vice versa.

Of course, the code is naive and a programmer may easily find and break the cycle. But if you have multiple shared objects passing from one class to another you can get in this situation. But what happens when we use them together? We know that there were no reference cycles in the function and after exiting there are no instances of shared pointers. I prefer not thinking in absolute terms like "last resort" but relative to the problem domain.

Actually you can: use pure value semantics. Works for relatively small objects where what is important are "values" and not "identities", where you can assume that two Person having an same name are the same person better: two representation of a same person.

Lifetime is granted by the machine stack, end -essentially- deosn't matter to the program since a person is it s name , no matter what Person is carrying it use stack allocated objects , and related reference or pointers: allows polymorphism, and grants object lifetime.

No need of "smart pointers", since you ensure no object can be "pointed" by structures that leave in the stack longer than the object they point to first create the object, then the structures that refer to it.

Again, you admit the object begin to exist and end their existence before after the data structure they refer to. In this cases, you have to either: introduce some discipline about managing object and related referring structures or And must leave This works the most of the time but fails in large design, where many designer works in different teams and there is no clear reason something coming from a somewhat requirement about who musty own what the typical example are dual liked chains: is the previous owing the next referring the previous or the next owning the previous referring the next?

In absebce of a requirement the tho solutions are equivalent, and in large project you risk to mix them up Use a garbage collecting heap : You simply don't care bout lifetime. You run the collector time to time and what is unreachabe is considered "not anymore needed" and The most of them free memory, not caring about object destruction. Good luck to find it. Emilio Garavaglia Emilio Garavaglia 4, 1 1 gold badge 20 20 silver badges 23 23 bronze badges.

Eclipse Eclipse 1 1 silver badge 3 3 bronze badges. To me this is the least important detail. See premature optimization. In most cases this shouldn't drive the decision. In a game project I worked on we found that the performance difference was very significant, to the point where we needed 2 different types of ref-counted pointer, one which was threadsafe, one which was not. Show 1 more comment. Eddie Velasquez Eddie Velasquez 4 4 bronze badges.

Dimitrios Menounos Dimitrios Menounos 5 5 bronze badges. This is only for advanced programmers because it requires that you understand how dependencies work, and requires ability to control dependencies enough to restrict them to a tree.

Order of classes in header file is important aspect of this. This solution also have problems with sizeof. Many programmers want to create factories for creating new objects. The reference based solution can work if the returned object is stored inside the containing object, instead of using local function scope variable. Passing ownership from factory to user code should be avoided. You just need to choose if your memory is inside the object, or outside of it, and choose the struct solution or reference solution based on the decision.

Problems with this solution are usually related to avoiding constructor parameters which is common but problematic practise and misunderstanding how interfaces for classes should be designed. Pointers can be reseated. References cannot. This forces construction-time restrictions on what is stored in a class. That's not practical for a lot of things. Your solution seems to be very rigid and inflexible to the needs of a more fluid interface and use pattern.

Nicol Bolas: Once you follow the rules above, the refs are going to be used for dependencies between objects, and not for data storage like you suggested. The dependencies are more stable than the data, so we never get into the problem you were considering. Here's a very simple example.

You have a game entity, which is an object. This is surprisingly and counter-intuitively effective, I have found. If this function is called a lot this will have measurable -ve impact. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 11 years, 2 months ago. Active 5 years, 10 months ago.

Viewed 37k times. John Kugelman k 65 65 gold badges silver badges bronze badges. Ian Ian 2 2 gold badges 7 7 silver badges 7 7 bronze badges. It also depends on the implementation used. Without the code to test it, the results can't be taken seriously. VS has lots of issues not yet production quality IME too. Can you shared the source somewhere?

If the raw pointer version isn't freeing the memory, of course it'll be much faster. It is a problem, I am afraid I can not. It works with complete topological model and has more than 40 classes Show 3 more comments. Active Oldest Votes.



0コメント

  • 1000 / 1000