c++ - Leaking resource from a RAII object -
i'm reading scott meyers' effective c++ , i'm @ item 15, providing access raw resource in resource-managing classes. here example:
class font { // raii class public: explicit font(fonthandle fh) // acquire resource; : f(fh) // use pass-by-value, because {} // c api ~font() { releasefont(f ); } // release resource ... // handle copying (see item14) private: fonthandle f; // raw font resource };
he proposed introduce explicit conversion member function getting access raw resource:
class font { public: ... fonthandle get() const { return f; } // explicit conversion function ... };
here said:
some programmers might find need explicitly request such conversions off-putting enough avoid using class. that, in turn, would increase chances of leaking fonts, thing font class designed prevent.
i didn't understand how providing acces raw-resource increase chances of leaking fonts? returned copy of raw pointer resource object. , shouldn't worry accessing dangle pointer acquired get
member function, becuase delete operator call if go out of scope.
what did miss?
think it, if can access resource nothing stop doing whatever want resource. may copy it, destroy it, recreate or whatever. without using class prevent resource leak. if recreate it, or copy have access unmanaged resource increasing risk of leak. , if destroy it, may create big mess in code
Comments
Post a Comment