What happens if the CImage :: Load method fails?

Will the CImage pixel be changed if it tries to load another image and the loading method fails?

+4
source share
1 answer

Exclusion guarantees are not documented, so you cannot accept anything.

Looking at the source code for CImage::Load(atlimage.h), it is found that the inability to download the image can destroy the previously saved image. Both shipments Loadultimately cause CImage::CreateFromGdiplusBitmap. This member of the class performs two operations: 1.) It calls Createto create a new storage for the image. This operation is destructive. 2.) It copies the original image data to the destination. This operation may fail.

Since you have an operation that may fail after an operation that destroys any previous data, you cannot assume that a failure to load the image will save the previous image data.

The following code implements the loading of a non-destructive image, first loading the image in a temporary place and assigning it to the destination only with success:

HRESULT NonDestructiveLoad( CImage& img, const CString& fileName ) {
    CImage tempImage;
    HRESULT hr = tempImage.Load( fileName );
    if ( SUCCEEDED( hr ) ) {
        // The following operations cannot fail
        img.Destroy();
        img.Attach( tempImage.Detach() );
    }
    return hr;
}
+4

Source: https://habr.com/ru/post/1613007/


All Articles