I have a class that just creates a blank bitmap it's quite large so I removed a LOT! of code to make it easier to see my problem. No knowledge of bitmaps required. I declared "Pixels" as an array of RGB but in one function, Pixels is empty whereas in other functions I initialize Pixels using the new operator.

In the destructor, I delete[] Pixels. But it will throw an error if it isn't initialized.

typedef union
    unsigned Color;
        unsigned char B, G, R, A;

class Bitmap
        HDC DC;
        PRGB Pixels;
        HBITMAP Image;
        BITMAPINFO Info;
        int width, height, size;
        BITMAPFILEHEADER bFileHeader;

        Bitmap(int Width, int Height)
            memset(&Info, 0, sizeof(BITMAPINFO));
            Info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
            Info.bmiHeader.biPlanes = 1;
            Info.bmiHeader.biBitCount = 32;
            Info.bmiHeader.biCompression = BI_RGB;
            Info.bmiHeader.biWidth = width;
            Info.bmiHeader.biHeight = -height;
            size = ((width * 32 + 31) / 32) * 4 * height;
            DC = CreateCompatibleDC(0);
            Image = CreateDIBSection(DC, &Info, DIB_RGB_COLORS, (void**)&Pixels, 0, 0);
            SelectObject(DC, Image);
            //I've tried Pixels = new RGB[100];  Doesn't work since it's still blank.

        /* Other bitmap functions here that does Pixels = new RGB[...]; */

            if (Pixels)
                delete[] Pixels;

int main()
    Bitmap X(100, 100);   //Crashes my program due to Pixels being deleted when not initialized.
    Bitmap Y("C:/Users.......");  //Will not crash my program since pixels is initialize with new[];
    return 0;

Any ideas what I can do so that I do not have to remove it from my destructor? Or is there a way to make Pixels without usingnewso that I can use it later? I doubt using Vector would be good in this case.. am I right?

Edited by triumphost

4 Years
Discussion Span
Last Post by triumphost
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.