JPEG Thumbnailer
Ingo Klöcker
kloecker at kde.org
Tue Aug 19 21:41:18 BST 2008
On Tuesday 19 August 2008, Thiago Macieira wrote:
> Ingo Klöcker wrote:
> >The point of using QFile is that you do not have to take care of the
> >file resource yourself. QFile will take care of closing the file.
> > Using a class for handling a resource (e.g. QFile for a file, smart
> > pointers for objects allocated on the heap, etc.) is always
> > preferable to dealing with the resource yourself, because it's so
> > darn easy to leak a resource if you do it yourself.
>
> That means you shouldn't use fdopen on the handle returned by QFile.
>
> The handle that QFile returns is closed correctly by QFIle, but if
> you do an fdopen, then you have to remember to fclose.
>
> So, if you need the FILE* anyways, avoid the complexity and open it
> directly.
I know you will kill me for making the following suggestion. :-)
If QFile is too much overhead then I'd use a boost::shared_ptr with
custom deleter [1], i.e.
boost::shared_ptr<FILE> fd_in( fopen(path.toLatin1().data(), "rb"), fclose );
if ( fd_in.get() == NULL )
return false;
If you want to close the file explicitly then you call fd_in.reset().
Otherwise you don't have to do anything.
Reasons for doing the above:
a) We are coding in C++, right? So let's use the power C++ provides. It
makes no sense to write error prone code, if there's a way to avoid it.
b) Using boost::shared_ptr is just a compile time dependency.
c) Several KDE packages (kdepimlibs, kdepim) do already have a
dependency on boost.
Regards,
Ingo
[1] http://www.boost.org/doc/libs/1_36_0/libs/smart_ptr/sp_techniques.html#handle
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 194 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20080819/27e51503/attachment.sig>
More information about the kde-core-devel
mailing list