Object pool pattern: Difference between revisions

Content deleted Content added
Reversed the main premise of this passage. Automated object pools are possible in C++ via smart pointers; Only manual possible in Java/C# as finalizers are really a bad idea.
Fixed up a grammatical error and added suggestion to use try/finally
Line 34:
 
== Implementation ==
Object pools can be implemented in an automated fashion in languages like C++ via smart pointers. In the constructor of the smart pointer - an object can be requested from the pool and in the destructor of the smart pointer - the object can be released back to the pool. In garbage collected languages, where there are no destructors (which are guaranteed to be called as part of a stack unwind) - object pools MUST be implemented in a manual fashion, by explicitly requesting an object from the [[Factory (object-oriented programming)|factory]] and returning the object viaby calling a dispose method, (as in the [[dispose pattern]]). Using a [[finalizer]] to do this is not a good idea as there are usually no guarantees on when (or if ever) the finalizer will be run. Instead - prefer using try ... finally to ensure that getting and releasing the object is exception neutral.
 
Manual object pools are simple to implement, but harder to use, as they require [[manual memory management]] of pool objects.