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;
    struct
    {
        unsigned char B, G, R, A;
    };
} RGB, *PRGB;

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

    public:
        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[...]; */

        ~Bitmap()
        {
            if (Pixels)
                delete[] Pixels;
            DeleteDC(DC);
            DeleteObject(Image);
        }
};


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?

What happenes if you set pixels to null in your constructor?

Pixels = NULL;

It still crashes if I do that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.