Explicitly calling destructor.
The documentation says:
Quote:
You are responsible for deleting the GDI bitmap and the GDI palette. However, you should not delete the GDI bitmap or the GDI palette until after the GDI+ Bitmap object is deleted or goes out of scope.
My code says:
Code:
Bitmap bmpPdf(hBitmap, NULL);
....
bmpPdf.~Bitmap();
DeleteObject(hBitmap);
I'm wondering:
- Is calling the destructor explicitly the equivalent of the object going out of scope?
- Is this acceptable practise?
- Is this common as I haven't seen it before?
Thanks.
Re: Explicitly calling destructor.
Quote:
Originally posted by anonytmouse
I'm wondering:
- Is calling the destructor explicitly the equivalent of the object going out of scope?
not really as the object remains in memory, but initially, calling the destructor explicitly will work anyways -- sort of.
The problem is that while you are explicitly calling the destructor, it will still be automatically called again once the object goes out of scope. That might not seem like too much of a problem but it is actually very big -- when the destructor is called the second time, the object is in an invalid state.
For instance, normally in the destructor it may call a function on an object pointed to through a datamember, which will tell that object that your bitmap is now destructed. If the destructor gets called twice (as it will in your example), that will obviously cause problems. Generally you should only call the destructor explicitly if you are within a member function of that class or if its from another object who is closely tied to the implementation of the type of object whose destructor is being called. Otherwise you will have problems as you can't be certain of the state of the object after the initial destruction.
Instead, if you want your bitmap to be destructed before your call, just put it in its own block:
Code:
{
Bitmap bmpPdf(hBitmap, NULL);
/*
stuff
*/
}
DeleteObject(hBitmap);
that way you are guaranteed bmpPdf is destructed before DeleteObject is called.