1

If I have a class with private constructors and destructors, is it still dangerous to pass classic c-style pointers to instances (as in opposed to shared_ptr)? Are there any situations where memory could leak? The lifetime of instances in my program is managed by a friend factory class exclusively (which also uses a private custom deleter to allow shared_ptr objects to be used internally by the manager class).

Dan Nestor
  • 2,441
  • 1
  • 24
  • 45
  • No, not at all. Why would it be dangerous to pass pointers around? That's what they're for. `shared_ptr` uses raw pointers internally anyway. And memory leaks have nothing to do with the private-ness or public-ness of constructors. – Seth Carnegie Nov 24 '11 at 18:34
  • @SethCarnegie I was thinking that with public constructors clients would be able to instance the class, and passing normal pointers instead of shared_ptr could leak memory. With private constructors this cannot happen. – Dan Nestor Nov 24 '11 at 18:42

1 Answers1

1

Constructors do not matter at all if you already have the pointer. As for destructors — if you have a private one, you won't be able to delete is and, I believe, that will also prevent you from instantiating shared_ptr (as dandrestor pointed out — unless you provide a custom deleter, which you can use not only with shared_ptr).

All the limitations are imposed at compile-time, so if you fail to free your memory because of privacy issues you will be notified at compile-time.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • you believe rigth: if the dtor is private the shared_ptr dtor, that has to call it, cannot be compiled. – Emilio Garavaglia Nov 24 '11 at 18:37
  • 3
    Actually, you can compile shared_ptr if you pass a custom deleter function. See http://stackoverflow.com/questions/8202530/how-can-i-call-a-private-destructor-from-a-shared-ptr – Dan Nestor Nov 24 '11 at 18:39
  • @EmilioGaravaglia, if we forget dandrestor's comment, I said "I believe" because I'm very cautious about making statements about the things I haven't tried — purely theoretical :) – Michael Krelin - hacker Nov 24 '11 at 18:45
  • @MichaelKrelin-hacker what about run-time problems? My reasoning is that, since construction and destruction are handled separately, using normal pointers does not present a risk. What do you think? – Dan Nestor Nov 24 '11 at 18:47
  • @MichaelKrelin-hacker In my program, the custom deleter is a static private method of the manager class, so this restricts destruction to the manager class. – Dan Nestor Nov 24 '11 at 18:48
  • @dandrestor, only now I realized that you *are* the OP :)) Yes, using normal pointers does not present a risk related to privacy of construction and destruction. Not at runtime, at least. They also give benefit of being able to provide a pointer to the object allocated in stack for instance in member or friend functions :) – Michael Krelin - hacker Nov 24 '11 at 18:51
  • But… if the manager class is on friendly terms with the suspect, why would it need a private custom deleter? – Michael Krelin - hacker Nov 24 '11 at 18:53
  • @MichaelKrelin-hacker Hah... because friendship does not propagate to its members. In my case, I use a vector of shared_ptrs and shared_ptr is not friends with the class in question. – Dan Nestor Nov 24 '11 at 19:00
  • Ah, right, what I was thinking of. I'm thinking… can't you befriend shared_ptr? – Michael Krelin - hacker Nov 24 '11 at 19:44
  • No, because shared_ptr is in itself a typedef or something else, anyway, it's a different class that actually ends up deleting the pointer. I reckon this is the exact reason for which they created the "deleter function" mechanism. – Dan Nestor Nov 24 '11 at 22:03