c++ - Gdiplus::Bitmap::FromHBITMAP memory leakage -


i call repeatedly code getting memory leakage:

ulong_ptr gdiplustoken; int screen_height; int screen_width;  cvcamstream::cvcamstream(hresult *phr, cvcam *pparent, lpcwstr ppinname) : csourcestream(lpcstr(filter_name),phr, pparent, ppinname), m_pparent(pparent) {     hdc = getdc(null);      gdiplus::gdiplusstartupinput gdiplusstartupinput;     ulong_ptr gdiplustoken;     gdiplusstartup(&gdiplustoken, &gdiplusstartupinput, null);      screen_height = getsystemmetrics(sm_cyvirtualscreen);     screen_width = getsystemmetrics(sm_cxvirtualscreen); }  cvcamstream::~cvcamstream() {     gdiplus::gdiplusshutdown(gdiplustoken);     deletedc(hdc); }   hresult cvcamstream::fillbuffer(imediasample *pms) {     reference_time rtnow;     reference_time avgframetime = ((videoinfoheader*)m_mt.pbformat)->avgtimeperframe;      static clock_t refclock = clock();     double elapsed = (clock() - refclock) / (double)clocks_per_sec;      rtnow = m_rtlasttime;     m_rtlasttime += avgframetime;     pms->settime(&rtnow, &m_rtlasttime);     pms->setsyncpoint(true);      hdc memdc = createcompatibledc(null);      cimage image;     image.create(screen_width, screen_height, 24);     selectobject(memdc, image);      byte *pdata;     pms->getpointer(&pdata);     long ldatalen = pms->getsize();      gdiplus::bitmap *bitmap = new gdiplus::bitmap(screen_width, screen_height, pixelformat24bpprgb);      bitblt(memdc, 0, 0, screen_width, screen_height, hdc, 0, 0, srccopy);     deletedc(memdc);      // memdc deleted     bitmap->fromhbitmap(image, null);      delete bitmap;     image.destroy();     deleteobject(image);     deletedc(memdc);      return noerror; }  

but every time ram usage increases. offending line fromhbitmap() function because commenting there no more leakage.

i know fromhbitmap() creates copy of bitmap thought free memories!

what's wrong here?

fromhbitmap not take ownership of passed bitmap handle. can pass bitmap handle still owned cimage instance. according msdn:

do not pass bitmap::fromhbitmap method gdi bitmap or gdi palette (or previously) selected device context.

and calling while image still selected memdc.


Comments

Popular posts from this blog

Ansible warning on jinja2 braces on when -

Parsing a protocol message from Go by Java -

html - How to custom Bootstrap grid height? -