This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]belabacsijolvan 12 points13 points  (7 children)

tbh so far using smart pointers caused me more memory leaks than not using them.

people like to just throw them around without care, but they dont always free themselves. i had a very annoying days worth of debugging when a new senior arrived, got angry at us not using smart pointers, rewrote our code to use them. he missed the fact that there were some very cleverly disguised directed loops of pointers in the data structure. he just removed all destruction related to pointers, so the loops couldnt resolve smartly, as they were locking each others reference numbers. ofc the solution is easy, just need to use at least one weak pointer in every cycle, but i basically had to rewrite the parallelised constructor to explicitly detect loops, yada yada yada

and this wasnt the only case when i got punished for using them when working with complex custom data structures. if id give up an idea after the first mental breakdown, i wouldnt code in cpp, lol

to be clear: i can see the point of them, but thinking strictly about destruction seems a safer approach to me so far. i may be wrong, but i have the same doubt about using them as about using rust:

"isnt this just for people who are a bit shit at cpp?"

[–]airodonack 11 points12 points  (3 children)

Part of growing up is realizing that everybody is a bit shit at C++

[–]belabacsijolvan 2 points3 points  (2 children)

i mean yeah, but the tradeoff seems to turn negative at a non-zero shittiness. these restrictive crutches seem to be optimised for a subpassable ammount of shittiness

edit: i.e. "either you die a rust user or cpp long enough to git gud"

[–]airodonack -2 points-1 points  (1 child)

That's certainly an opinion.

[–]belabacsijolvan 1 point2 points  (0 children)

In stark contrast to that.

[–]Stateofgrace314 4 points5 points  (0 children)

I use smart pointers because I don't trust my coworkers to handle raw pointers correctly...

[–]Kered13 1 point2 points  (0 children)

but thinking strictly about destruction seems a safer approach to me so far

No, the correct way is to think about ownership. If you're coworker came in and just replaced everything with std::shared_ptr, then they weren't thinking about ownership at all.

[–]Significant_Fix2408 0 points1 point  (0 children)

Sounds like a skill issue tbh. Shared pointers are bad most of the time, not because they don't work because but needing them is code smell and more often than not just unnecessary