0

I have a problem similar to this one: How to copy a CImage object? I need to make a deep copy of CImage object. The specific problem is that I'm getting a CImage* from a secondary thread through PostMessage, and want to copy it to CImage object which is an attribute of different class.

i currently have following code:

LRESULT CMainFrame::TransApplied(UINT wParam,LONG lParam)
{
    DEB("message recieved\n"); //this is debug message to output
    CImage *obrazek=(CImage* )wParam;
    int a;
    CString t;
    CImage tymczas;
    obrazek->Save(_T("mesydz.jpg"));//checking if the image is valid
    DEB("o1\n");
    obrazek->GetDC();
 PointerToViewClass->przetransformowany.Create(obrazek->GetWidth(),obrazek->GetHeight(),obrazek->GetBPP());
    DEB("o2\n");
     obrazek->BitBlt(PointerToViewClass->przetransformowany.GetDC(),0,0,SRCCOPY);


//  obrazek->ReleaseDC();
    DEB("o8\n");
     PointerToViewClass->przetransformowany.ReleaseDC();
    DEB("o6\n");

    DEB("o7\n");
    obrazek->ReleaseDC();
    //delete obrazek;
    return 0;
}

i suspect there is something wrong with obrazek->GetDC() and obrazek->ReleaseDC(). i have no errors when i have obrazek->GetDC() (but otherwise there is debug assertion failed on obrazek->BitBlt(...); ), though I am unable to release obrazek's DC and/or delete the obrazek object, which i would like to do, because i am having memory leaks if i don't do it.

so in general there is something i don't know about copying the object.

EDIT:

the other thread handles the CImage in the following way:

   CImage wyniczek;   
   CImage *doprzeslania;

   objekt->ApplyTransform(macierz,&wyniczek);//object is an object of a class declared in dll

  doprzeslania=new CImage(wyniczek);

   PostMessage(data->parent,MY_WM_MESSAGE4,(WPARAM)doprzeslania,(LPARAM)3);

 delete objekt;
 FreeLibrary(hDLL);
 delete data;//deleting an object with settings for the thread
 return 0;

and additionally, what is happening in the dll with the image:

void IProcess::ApplyTransform(std::vector<double> matrix,CImage *rezult)
{
//
//some image processing here
//
output.Create(dest.cols,dest.rows,24);
BITMAPINFO bmi; //it is initialized properly, had erased it for clarity
StretchDIBits(output.GetDC(),0,0,dest.cols,dest.rows,0,0,dest.cols,dest.rows,temp.data,&bmi,DIB_RGB_COLORS,SRCCOPY); //copy from opencv Mat object to CImage

rezult->Create(output.GetWidth(),output.GetHeight(),24);

 output.BitBlt(rezult->GetDC(),0,0,SRCCOPY); //copy to CImage *rezult

output.ReleaseDC();
rezult->ReleaseDC();
}
Community
  • 1
  • 1
user3621602
  • 65
  • 1
  • 8
  • 1
    Are you really sure that the object still exists when you receive the message. How does the other thread frees this CImage? – xMRi Oct 09 '14 at 08:50
  • yes, i'm pretty sure - the CImage is succesfully saved to a disk (obrazek->Save(_T("mesydz.jpg")); ) and seems to be completely valid image. concerning other thread i will modify the question and add more code. – user3621602 Oct 09 '14 at 16:49
  • or maybe there exists an alternative way of exchanging images between classes and threads? – user3621602 Oct 10 '14 at 18:39

1 Answers1

0

Finally i came up with what was going wrong here!

The problem was, that in secondary thread i was returning the CImage object into another CImage object (which was held on the stack):

CImage wyniczek;
objekt->ApplyTransform(macierz,&wyniczek);

and then copy it with copy constructor to a new object constructed on the heap:

CImage *doprzeslania;
doprzeslania=new CImage(wyniczek);

so in general, the *doprzeslania object was valid only as long as the secondary thread was running, which resulted in weird behaviour that i was able to properly save it at the begining of the function which was recieving PostMessage, but was unable to copy it to another object. The solution was to get the image into *doprzeslania object directly from the function:

CImage *doprzeslania;
doprzeslania=new CImage();
objekt->ApplyTransform(macierz,doprzeslania);

Now i have problem with showing this image on the screen, but before i'll ask another question i will have to look into it closer by myself. Thanks everyone for asistance!

user3621602
  • 65
  • 1
  • 8