Thread: Smart pointers was RE: [GD-General] Compile times
Brought to you by:
vexxed72
From: brian h. <bri...@py...> - 2002-12-16 02:16:40
|
> Hmm, I've seen smart pointers used on several projects and they have > never caused any problems. What sort of "ugly things" are you > referring to? Assuming that we're defining "smart pointer" as ref-counted, templatized pointer access, then here are the typical bugaboos: - non-deterministic destruction (a problem with general GC as well) - extremely slack model of ownership (a problem with general GC as well) - cascading destruction if you deref a root node - array support can be iffy depending on the implementation - cycles - bugs that arise if your implementation doesn't properly handle stack, static/global variables, or "special" memory that requires its own de- allocation (pointer to surfaces, pointer to AGP memory, etc.) All that said, there are some pretty solid implementations (Boost?) out there that I believe address at least the key issues. My gripes are generally more of the subjective stuff -- I think it obfuscates code, lends to programmer laziness because there's a poor concept of memory ownership and, deep down inside (okay, maybe right there at the surface), I just feel that retrofitting automatic memory management on a language that was clearly designed for manual memory management is a Bad Idea. -Hook |
From: Jesse J. <jes...@mi...> - 2002-12-16 12:06:06
|
At 8:16 PM -0600 12/15/02, brian hook wrote: > > Hmm, I've seen smart pointers used on several projects and they have >> never caused any problems. What sort of "ugly things" are you >> referring to? > >Assuming that we're defining "smart pointer" as ref-counted, >templatized pointer access, then here are the typical bugaboos: Well reference counted smart pointers are probably the most often used, but there are also things like std::auto_ptr (useful with exceptions) and somewhat more esoteric things (for example I use a smart pointer class for preference data that I am very happy with). >- non-deterministic destruction (a problem with general GC as well) How so? With a real GC objects aren't deleted until the collector gets around to freeing them, but with all the ref-counted smart pointer classes I've seen they are deleted when the last reference is released. >- extremely slack model of ownership (a problem with general GC as well) That's a feature. :-) >- cascading destruction if you deref a root node Why is this a problem? >- array support can be iffy depending on the implementation Nah, this is easy to handle as long as you properly define the smart pointer's copy ctor and assignment operators (and your array classes are coded correctly). >- cycles Yeah, that can be a pain but it's not really an issue with smart pointers per se. >- bugs that arise if your implementation doesn't properly handle stack, >static/global variables, or "special" memory that requires its own de- >allocation (pointer to surfaces, pointer to AGP memory, etc.) These are all non-issues in my experience (even the last one: you don't delete a surface for example, you delete a class that wraps surfaces). >All that said, there are some pretty solid implementations (Boost?) out >there that I believe address at least the key issues. boost:shared_ptr is a good class to look at. >My gripes are generally more of the subjective stuff -- I think it >obfuscates code, lends to programmer laziness because there's a poor >concept of memory ownership and, deep down inside (okay, maybe right >there at the surface), I just feel that retrofitting automatic memory >management on a language that was clearly designed for manual memory >management is a Bad Idea. Well, what are the options? In my experience automating reference counting with smart pointers is a hell of a lot better than doing it manually. Doing all the memory management manually with naked calls to new and delete is feasible with discipline and some programming conventions, but there are usually a few instances where this becomes difficult. -- Jesse |
From: Thatcher U. <tu...@tu...> - 2002-12-16 16:46:40
|
On Dec 16, 2002 at 04:05 -0800, Jesse Jones wrote: > At 8:16 PM -0600 12/15/02, brian hook wrote: > >Assuming that we're defining "smart pointer" as ref-counted, > >templatized pointer access, then here are the typical bugaboos: [snip] > Well, what are the options? Brian and Ignacio mentioned handles. It seems to me that handles are equivalent to smart pointers, with a few differences in details. The similarities: * If your handle manager keeps a ref-count (or something similar) to avoid deleting an object that has an active handle, then handles essentially behave like ref-counted smart pointers. * If your handle manager is allowed to delete objects when it feels like it, then handles are essentially the same as weak pointers, with a strong pointer in the handle manager. And the differences: * Weak pointers are bigger than handles (the implementation we use has an object pointer plus two list link pointers). * Handles are hard to debug! * Handles involve a little extra plumbing in the client, to extract a real pointer. -- Thatcher Ulrich http://tulrich.com |
From: Jesse J. <jes...@mi...> - 2002-12-17 11:52:31
|
At 11:42 AM -0500 12/16/02, Thatcher Ulrich wrote: >On Dec 16, 2002 at 04:05 -0800, Jesse Jones wrote: >> At 8:16 PM -0600 12/15/02, brian hook wrote: >> >Assuming that we're defining "smart pointer" as ref-counted, >> >templatized pointer access, then here are the typical bugaboos: > >[snip] > >> Well, what are the options? > >Brian and Ignacio mentioned handles. It seems to me that handles are >equivalent to smart pointers, with a few differences in details. The >similarities: > >* If your handle manager keeps a ref-count (or something similar) to > avoid deleting an object that has an active handle, then handles > essentially behave like ref-counted smart pointers. > >* If your handle manager is allowed to delete objects when it feels > like it, then handles are essentially the same as weak pointers, > with a strong pointer in the handle manager. This seems right to me and handles seem like a good option if you don't have enough memory to hold all of your objects and you can re-construct them from a resource file or some sort of save file. -- Jesse |