Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: What about memory leaks?  (Read 22286 times)

0 Members and 1 Guest are viewing this topic.

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: What about memory leaks?
« Reply #45 on: June 07, 2015, 10:02:28 am »
That's a nice bit of reductio ad absurdum there. Waiting until someone makes mistakes by not using smart pointers doesn't mean that everyone who didn't use them made mistakes. It means it is possible and therefore you might make a mistake by not using them. If you want examples of try-catch with memory management look at this http://www.bromeon.ch/articles/raii.html Also, in my experience smart pointers can act exactly as normal pointers and when you're using unique_ptr they have no overhead so I can see no reason not to use them except disliking change. And yes, unique_ptr<int> isn't of type int, but neither is int*. Both use the same operators to access the data they point to but unique_ptr has other functions which can be useful. You can even access the underlying pointer directly if you want to.

Jabberwocky

  • Full Member
  • ***
  • Posts: 157
    • View Profile
Re: What about memory leaks?
« Reply #46 on: June 07, 2015, 12:42:07 pm »
That's a nice bit of reductio ad absurdum there.

I think your reductio ad absurdum is a strawman.  ;)
Heh, actually I get why you're saying that, but it's really this simple.

Rosme said this:
Quote
I can't do anything but wait until you have an issue with your pointers.
...obviously implying issues was either likely or practically inevitable.

My response refuted this - that it is neither likely nor practically inevitable.

I'm pretty sure that's all either of us meant.

Waiting until someone makes mistakes by not using smart pointers doesn't mean that everyone who didn't use them made mistakes. It means it is possible and therefore you might make a mistake by not using them.

You can't write a line of C++ code without possibly making a mistake.  Of course mistakes can be made.  All C++ programmers have a toolbox and mental process to address this.  Mine is different than yours.  That doesn't make me incorrect.

shadowmouse, the rest of your post mostly repeats arguments that have already been made, and already responded to.  (I don't blame you for that, it's getting to be a long thread.)  We did give some reasons for not using smart pointers - there's more to it than disliking change.  We did give some reasons why those exception/memory leak examples are not a real issue.  But you'll have to critique that stuff if you want to cover some new ground here, instead of just talking in circles.

Either way, thanks for chiming in!

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: What about memory leaks?
« Reply #47 on: June 07, 2015, 12:47:46 pm »
Morko did ask for examples of code involving exceptions so the main point of my post was the link. But I'd still say that waiting until you have a problem means it only really because benficial if you have a problem, which may or may not happen.

EDIT: Also,
Quote
Every C program must be a horrible leaking mess right?
is reductio ad absurdum because it was never said that all programs with smart pointers have mistakes in them and the extrapolation that because smart pointers can stop issues, not having them means there must be issues is illogical.
« Last Edit: June 07, 2015, 12:51:31 pm by shadowmouse »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: What about memory leaks?
« Reply #48 on: June 07, 2015, 03:46:58 pm »
I expected there would be more resistance to storing a secondary raw pointer to a unique pointer (edit - sounds like Jesper would be against this approach).  It's not really "unique" then.  But agree in this case it would work.
I wouldn't be opposed to obtaining a raw pointer to a unique_ptr (or rather; a raw pointer to what the unique_ptr points to), not even to passing it around, as long as it is clear that the raw pointer is not owning the resource pointed to, but merely inspecting/using it. It's all about ownership semantics.
« Last Edit: June 07, 2015, 05:02:30 pm by Jesper Juhl »

Mörkö

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: What about memory leaks?
« Reply #49 on: June 07, 2015, 05:06:28 pm »
Quote
Every C program must be a horrible leaking mess right?
is reductio ad absurdum because it was never said that all programs with smart pointers have mistakes in them and the extrapolation that because smart pointers can stop issues, not having them means there must be issues is illogical.
That line was a joke, not a serious argument, which is why it should seem absurd if you take it seriously.

Quote
And yes, unique_ptr<int> isn't of type int, but neither is int*. Both use the same operators to access the data they point to but unique_ptr has other functions which can be useful. You can even access the underlying pointer directly if you want to.
That's all true, but the argument here is that it does not merely represent the address of he object, but also brings in a whole bunch of new rules that the programmer must reason about. And also, it changes the memory management mechanism in a way which some of us find undesirable.
« Last Edit: June 07, 2015, 05:10:58 pm by Mörkö »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: What about memory leaks?
« Reply #50 on: June 11, 2015, 03:21:44 pm »
There's so much half-knowledge and plain wrong statements in this thread. "Unique pointers have overhead", "using manual memory management is a matter of taste", "exceptions are not a real issue" and so on. Please stop spreading myths. Beginners read this and are misled all the time, contributing to the main reason why most C++ code is still terrible in 2015.

A while ago I compiled a pretty exhaustive list of arguments for RAII. You can read the article here:
www.bromeon.ch/articles/raii.html
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

ChronicRat

  • Sr. Member
  • ****
  • Posts: 327
  • C++ programmer
    • View Profile
    • My blog
Re: What about memory leaks?
« Reply #51 on: June 11, 2015, 04:13:16 pm »
Thanks, Nexus, great "debriefing".

 

anything