Sam
Sam

Reputation: 23

Saving GDI+ Bitmap after threaded operations on it

I'm doing some image processing on some images I load from file. I have a class that has Load, Execute and Save functions. I always Load and Save in the main thread in serial, but I'm threading Execute calls on different images. If I thread the execute the Save fails, if I don't thread the execute it works. I load an image, clone it 3 times then make changes to those and save them all. The filenames being saved to are not the same as the original image loaded in.

This works:

ProcessImage process1 = ProcessImage(L"IMG_1");
process1.Load();
process1.Execute();
process1.Save();

This doesn't:

ProcessImage process1 = ProcessImage(L"IMG_1");
process1.Load();
thread t1(&ProcessImage::Execute, process1);
t1.join();
process1.Save(); //exception

Exception in Image::Save GdipSaveImageToFile:

read access violation. this was 0xCCCCCCCC.

My save function looks like this:

   void Save() {
        CLSID pngClsid;
        CLSIDFromString(L"{557CF406-1A04-11D3-9A73-0000F81EF32E}", &pngClsid);

        destBright->Save(brightenOut.c_str(), &pngClsid); //exception
        delete destBright;

       //same again on 2 other Bitmaps
    }

destBright is a private member of ProcessImage that is a pointer to a Bitmap.

Thanks. Can provide more code if needed.

Upvotes: 1

Views: 138

Answers (1)

Sam
Sam

Reputation: 23

Solved

Changing the ProcessImage process1 to a pointer fixed the problem. Passing in the object and not a pointer to a std::thread seems to pass by value not reference, something I was unaware of. Someone else would have to confirm if that is correct.

Upvotes: 1

Related Questions