Click here to Skip to main content
14,661,630 members
Home » Discussions » C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Assert failure when I comment SetRegistryKey(_T("Local AppWizard-Generated Applications")) Pin
Richard MacCutchan27-Feb-16 21:08
mveRichard MacCutchan27-Feb-16 21:08 
JokeRe: Assert failure when I comment SetRegistryKey(_T("Local AppWizard-Generated Applications")) Pin
David Crow28-Feb-16 16:19
MemberDavid Crow28-Feb-16 16:19 
GeneralRe: Assert failure when I comment SetRegistryKey(_T("Local AppWizard-Generated Applications")) Pin
Richard MacCutchan28-Feb-16 21:25
mveRichard MacCutchan28-Feb-16 21:25 
QuestionRe: Assert failure when I comment SetRegistryKey(_T("Local AppWizard-Generated Applications")) Pin
David Crow28-Feb-16 16:20
MemberDavid Crow28-Feb-16 16:20 
Questionwhere "FindFirstFile" fetch drive information ? Pin
shanmugarajaa26-Feb-16 18:44
Membershanmugarajaa26-Feb-16 18:44 
AnswerRe: where "FindFirstFile" fetch drive information ? Pin
Richard MacCutchan26-Feb-16 21:53
mveRichard MacCutchan26-Feb-16 21:53 
AnswerRe: where "FindFirstFile" fetch drive information ? Pin
David Crow28-Feb-16 16:22
MemberDavid Crow28-Feb-16 16:22 
QuestionHow to dynamically update gdiplus bitmap object in MFC? Pin
Kiran Satish26-Feb-16 7:45
MemberKiran Satish26-Feb-16 7:45 
I have a MFC dialog application in which I have a gdiplus object that I use to draw images in real-time. Works fine if I don't want to do any changes to this object or its associated variables. But when I try to update them at runtime, it starts to increase my memory consumption.

So, my question is, how to safely update a gdiplus object at runtime.

Here is my code

// Variables declaration in header file	
LONG		Width;
LONG		Height;
LONG		bpp;
LONG		Stride;
BITMAPINFO*	bmi;
BYTE*		pDIBSectionBits;
HBITMAP		hbm;
Graphics*	imagedisp;
Bitmap*		offscreenBitmap;
CWnd* Display;
CStatic m_ImageDisp;

// Source file
OnInitDialog()
{
......
  Width = 512;
  Height = 512;
  bpp = 8;	
  Stride = ((Width * bpp + 31L) & (~31L)) / 8L;
......
}

InitParams()
{
  SetImageColor();	

  Display = GetDlgItem(IDH_IMAGE);
  this->GetClientRect(&m_disp_rect);	
  Display->GetClientRect(&m_disp_rect);
  m_ImageDisp.GetClientRect(&m_disp_rect);
  imagedisp = new Graphics(m_ImageDisp.GetDC()->GetSafeHdc());
}

SetImageColor()
{
  m_BUpdate = false;
  if (bmi != NULL)
  {
	delete bmi;
	bmi = NULL;
  }
  bmi = (BITMAPINFO *)new BYTE[sizeof(BITMAPINFO) + UCHAR_MAX * sizeof(RGBQUAD)];
  bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  bmi->bmiHeader.biWidth = Width;
  bmi->bmiHeader.biHeight = Height;
  bmi->bmiHeader.biPlanes = 1;
  bmi->bmiHeader.biBitCount = (WORD)bpp;
  bmi->bmiHeader.biCompression = BI_RGB;
  bmi->bmiHeader.biSizeImage = 0;//Stride * abs(Height);
  bmi->bmiHeader.biXPelsPerMeter = 0;
  bmi->bmiHeader.biYPelsPerMeter = 0;
  bmi->bmiHeader.biClrUsed = 0;
  bmi->bmiHeader.biClrImportant = 0;
  memcpy(bmi->bmiColors, USECOLORMAP?summercmap:greyscale, 256*sizeof(RGBQUAD));
  hbm = ::CreateDIBSection(NULL, bmi, DIB_RGB_COLORS, (void**)&pDIBSectionBits, NULL, 0);	
  if (offscreenBitmap != NULL)
  {
	delete offscreenBitmap;
	offscreenBitmap = NULL;
  }
  offscreenBitmap = new Bitmap(bmi, pDIBSectionBits);
  m_BUpdate = true;
}

OnPaint()
{
  CPaintDC dc(this); // device context for painting
  // TODO: Add your message handler code here
  if (m_BUpdate)
  {
	BYTE *pCurRowPixel = (BYTE *)(pDIBSectionBits);
		memcpy(pCurRowPixel, ImgBuff, Width*Height);
	
	imagedisp->DrawImage(offscreenBitmap, 0, 0, Width, Height);
  }
}


So whenever the user clicks a button to change between grayscale and color pallette, SetImageColor() is called. When I monitor the memory usage while clicking that button, I see its changing the way I expect it to, but the memory usage keeps increasing at a rate of ~300KB per click.

Is there any safer way to achieve what I am trying to do without memory leaks?

thanks
PKNT


modified 26-Feb-16 14:31pm.

AnswerRe: How to dynamically update gdiplus bitmap object in MFC? Pin
Chris Losinger26-Feb-16 8:44
professionalChris Losinger26-Feb-16 8:44 
GeneralRe: How to dynamically update gdiplus bitmap object in MFC? Pin
Kiran Satish26-Feb-16 9:55
MemberKiran Satish26-Feb-16 9:55 
Newserror:Segmentation fault (core dumped) Pin
Van Nguyen26-Feb-16 2:47
MemberVan Nguyen26-Feb-16 2:47 
GeneralRe: error:Segmentation fault (core dumped) Pin
Jochen Arndt26-Feb-16 3:04
professionalJochen Arndt26-Feb-16 3:04 
QuestionDifficulty in getting iterator upon using find algorithm of STL Pin
Member 1235353126-Feb-16 0:50
MemberMember 1235353126-Feb-16 0:50 
AnswerRe: Difficulty in getting iterator upon using find algorithm of STL Pin
Jochen Arndt26-Feb-16 0:59
professionalJochen Arndt26-Feb-16 0:59 
AnswerRe: Difficulty in getting iterator upon using find algorithm of STL Pin
Jochen Arndt26-Feb-16 1:55
professionalJochen Arndt26-Feb-16 1:55 
GeneralRe: Difficulty in getting iterator upon using find algorithm of STL Pin
Member 1235353126-Feb-16 2:30
MemberMember 1235353126-Feb-16 2:30 
GeneralRe: Difficulty in getting iterator upon using find algorithm of STL Pin
Jochen Arndt26-Feb-16 2:43
professionalJochen Arndt26-Feb-16 2:43 
GeneralRe: Difficulty in getting iterator upon using find algorithm of STL Pin
Member 123535317-Mar-16 19:23
MemberMember 123535317-Mar-16 19:23 
GeneralRe: Difficulty in getting iterator upon using find algorithm of STL Pin
Jochen Arndt7-Mar-16 21:41
professionalJochen Arndt7-Mar-16 21:41 
QuestionDLL Features Pin
bkelly1324-Feb-16 12:53
Memberbkelly1324-Feb-16 12:53 
AnswerRe: DLL Features Pin
Jochen Arndt24-Feb-16 21:24
professionalJochen Arndt24-Feb-16 21:24 
AnswerRe: DLL Features Pin
Richard MacCutchan24-Feb-16 21:34
mveRichard MacCutchan24-Feb-16 21:34 
Questioncontrol #define directive Pin
p3im4n24-Feb-16 4:34
Memberp3im4n24-Feb-16 4:34 
SuggestionRe: control #define directive Pin
Jochen Arndt24-Feb-16 4:57
professionalJochen Arndt24-Feb-16 4:57 
AnswerRe: control #define directive Pin
Richard MacCutchan24-Feb-16 8:25
mveRichard MacCutchan24-Feb-16 8:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.