Memory leaks in c++ dll [duplicate] - c++

This question already has answers here:
Why does CreateCompatibleBitmap fail after about a thousand executions?
(1 answer)
Capture screenshot Including Semitransparent windows in .NET
(1 answer)
Closed 9 years ago.
I have simple c++ dll that contains code for screen capturing.
HBITMAP hCaptureBitmap;
extern "C" __declspec(dllexport) HBITMAP __stdcall CaptureScreenByGDI(bool allScreens)
{
int nScreenWidth;
int nScreenHeight;
HDC hDesktopDC;
if(allScreens)
{
nScreenWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN);
nScreenHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN);
}
else
{
nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
}
HWND hDesktopWnd = GetDesktopWindow();
if(allScreens)
{
hDesktopDC = GetDC(NULL);
}
else
{
hDesktopDC = GetDC(hDesktopWnd);
}
HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC,
nScreenWidth, nScreenHeight);
SelectObject(hCaptureDC,hCaptureBitmap);
BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight,
hDesktopDC,0,0,SRCCOPY|CAPTUREBLT);
ReleaseDC(hDesktopWnd,hDesktopDC);
DeleteDC(hCaptureDC);
return hCaptureBitmap;
}
extern "C" __declspec(dllexport) void __stdcall ClearAfterGDI()
{
DeleteObject(hCaptureBitmap);
}
After calling
CaptureScreenByGDI(true);
ClearAfterGDI();
from c# still remain memory leaks. Why?
If I call DeleteObject(hCaptureBitmap) from CaptureScreenByGDI function and return void everthing is OK.
How to solve this?

You need to save the old bitmap in the DC you create and restore it before deleting the DC:
HGDIOBJ hBmpOld = SelectObject(hCaptureDC,hCaptureBitmap);
...
SelectObject(hCaptureDC, hBmpOld);
DeleteDC(hCaptureDC);

Related

ATL: OnDrawThumbnail hDrawDC seems to be monochrome in IThumbnailProvider

I'm working on a C++ ATL COM thumbnail/preview/search project, but its bitmap displaying code behavior is monochrome during the Thumbnail process instead of the colored. The Preview process is colored as expected, using the same function.
I used the ATL Wizard to create the IThumbnailProvider and his friends. My small changes are: I replaced the color from black to pink in the document::OnDrawThumbnail and I wrote the document::OnDrawThumbnail into CPreviewCtrl::DoPaint. I've read the "new DC always monochrome" thing in the MS spec but I could not get colored DC even if a changed the original ATL code OnDrawThumbnail(GetDC(NULL), &rcBounds);. The CreateCompatibleDC(NULL) and CreateCompatibleDC(hDrawDC) were dead-end too.
document.cpp (changed)
// Almost the default sample code, but hDrawBrush is changed to pink
void document::OnDrawThumbnail(HDC hDrawDC, LPRECT lprcBounds)
{
HBRUSH hDrawBrush = CreateSolidBrush(RGB(255, 0, 255)); // pink
FillRect(hDrawDC, lprcBounds, hDrawBrush);
HFONT hStockFont = (HFONT) GetStockObject(DEFAULT_GUI_FONT);
LOGFONT lf;
GetObject(hStockFont, sizeof(LOGFONT), &lf);
lf.lfHeight = 34;
HFONT hDrawFont = CreateFontIndirect(&lf);
HFONT hOldFont = (HFONT) SelectObject(hDrawDC, hDrawFont);
CString strText = _T("TODO: implement thumbnail drawing here");
DrawText(hDrawDC, strText, strText.GetLength(), lprcBounds, DT_CENTER | DT_WORDBREAK);
SelectObject(hDrawDC, hDrawFont);
SelectObject(hDrawDC, hOldFont);
DeleteObject(hDrawBrush);
DeleteObject(hDrawFont);
}
PreviewHandler.h (changed, it is called by the Preview)
// CPreviewCtrl implementation
class CPreviewCtrl : public CAtlPreviewCtrlImpl
{
protected:
virtual void DoPaint(HDC hdc)
{
// you can obtain a pointer to IDocument as follows
// CMyDoc* pDoc = (CMyDoc*)m_pDocument;
/*
CString strData = _T("Draw Rich Preview content here.");
TextOut(hdc, 10, 20, strData, strData.GetLength());
*/
RECT rc{};
rc.right = 290;
rc.bottom = 290;
dynamic_cast<document*>(m_pDocument)->OnDrawThumbnail(hdc, &rc);
}
};
atlhandlerimpl.h (unchanged, from VS SDK \atlmfc\include\ which is called be the thumbnail provider)
ATLPREFAST_SUPPRESS(6101)
_Success_(return != FALSE) BOOL GetThumbnail(
_In_ UINT cx,
_Out_ HBITMAP* phbmp,
_Out_ WTS_ALPHATYPE* /* pdwAlpha */)
{
HDC hdc = ::GetDC(NULL);
RECT rcBounds;
SetRect(&rcBounds, 0, 0, cx, cx);
HDC hDrawDC = CreateCompatibleDC(hdc);
if (hDrawDC == NULL)
{
ReleaseDC(NULL, hdc);
return FALSE;
}
HBITMAP hBmp = CreateCompatibleBitmap(hDrawDC, cx, cx);
if (hBmp == NULL)
{
ReleaseDC(NULL, hdc);
DeleteDC(hDrawDC);
return FALSE;
}
HBITMAP hOldBitmap = (HBITMAP) SelectObject(hDrawDC, hBmp);
// Here you need to draw the document's data
OnDrawThumbnail(hDrawDC, &rcBounds);
SelectObject(hDrawDC, hOldBitmap);
DeleteDC(hDrawDC);
ReleaseDC(NULL, hdc);
*phbmp = hBmp;
return TRUE;
}
ATLPREFAST_UNSUPPRESS()
Sample thumbnail in the File Explorer
Github helped me. It is definitely an ATL SDK bug.
BUG report on the VS developer community
Solution on the www.patthoyts.tk
And the github repo which helped me: abhimanyusirohi/ThumbFish
In the atlhandlerimpl.h provided GetThumbnail must be override:
BOOL document::GetThumbnail(
_In_ UINT cx,
_Out_ HBITMAP * phbmp,
_Out_ WTS_ALPHATYPE* /* pdwAlpha */)
{
BOOL br = FALSE;
HDC hdc = ::GetDC(NULL);
HDC hDrawDC = CreateCompatibleDC(hdc);
if (hDrawDC != NULL)
{
void* bits = 0;
RECT rcBounds;
SetRect(&rcBounds, 0, 0, cx, cx);
BITMAPINFO bi = { 0 };
bi.bmiHeader.biWidth = cx;
bi.bmiHeader.biHeight = cx;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biSizeImage = 0;
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biClrUsed = 0;
bi.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, &bits, NULL, 0);
if (hBmp != NULL)
{
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hDrawDC, hBmp);
OnDrawThumbnail(hDrawDC, &rcBounds);
SelectObject(hDrawDC, hOldBitmap);
*phbmp = hBmp;
br = TRUE;
}
DeleteDC(hDrawDC);
}
ReleaseDC(NULL, hdc);
return br;
}

Identifier is undefined when deleting a dynamic array [duplicate]

This question already has answers here:
Freeing memory error?
(2 answers)
Closed 3 years ago.
I am trying out a code to capture bitmap information using CreateDIBSection. Since I want to make the size of the array (which to hold each pixel value of the display monitor) flexible in order to suit monitor of different size, I create a dynamic unsigned char array (each color channel is 1 byte).
However, when I run the program, the program crashed right at the part of deleting the array, near the end of the program.
I tried to cast the array back to original type, i.e. unsigned char*, since I suspected it had been casted to void** when it being passed into CreateDIBSection(), but it doesn't work.
Below is the code, appreciate all the advice.
#include <Windows.h>
#include <cstdint>
HWND m_hwnd;
void GetBitMapInfo(const int& x_Coordinate, const int& y_Coordinate, const int& iWidth, const int& iHeight)
{
DWORD imageSize = iWidth * iHeight * 4;
// Get the display window
HDC displayWindow = GetDC(m_hwnd);
HDC hdc = CreateCompatibleDC(displayWindow);
// Fill in the Bitmap information
BITMAPINFO bmpInfo;
ZeroMemory(&bmpInfo, sizeof(BITMAPINFO));
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = iWidth;
bmpInfo.bmiHeader.biHeight = iHeight;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 32;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biSizeImage = 0;
bmpInfo.bmiHeader.biClrUsed = 0;
bmpInfo.bmiHeader.biClrImportant = 0;
// Create the storage for the pixel information
uint8_t* image = new uint8_t[imageSize];
// Populate the storage with the BMP pixel information
HBITMAP hBitmap = CreateDIBSection(hdc, &bmpInfo, DIB_RGB_COLORS, (void**)(&image), nullptr, NULL);
SelectObject(hdc, hBitmap);
BitBlt(hdc, x_Coordinate, y_Coordinate, iWidth, iHeight, displayWindow, 0, 0, SRCCOPY);
delete[] image; //Program crashed here: identifier "image" is undefined
image = nullptr;
DeleteDC(hdc);
DeleteDC(displayWindow);
DeleteObject(hBitmap);
return;
}
int main()
{
GetBitMapInfo(0, 0, 1920, 1080);
return 0;
}
The CreateDIBSection function make the pointer
... receives a pointer to the location of the DIB bit values.
The memory you have allocated for image is lost (a memory leak) and the new pointer can't be passed to delete[] (that will lead to undefined behavior).
The documentation states that with a null pointer hSection the memory allocated by CreateDIBSection will be free'd by DeleteObject.

CreateDIBSection() failed - how to diagnose

I have a call to CreateDIBSection() which is returning 0. The documentation says that this means that "One or more of the input parameters is invalid" but there is no indication of which parameter is invalid.
I had a look that this SO question in which the question ends with "after CreateDIBSection I found the error code is 8, no enough resource" - this confused me - there is no mention of any error codes in the documentation. How did the the poster get this extra information?
EDIT: As requested, here is my code - I must apologise it is not complete - it is part of a huge program, and making a minimal winapi program is not trivial.
HDC hdcTemp;
BYTE* bitPointer;
hdcTemp = CreateCompatibleDC(hdc_desktop);
my_printf("GetDeviceCaps(hdcTemp,BITSPIXEL) = %d\n",GetDeviceCaps(hdcTemp,BITSPIXEL)); // this prints "32"
static BITMAPINFO bitmap;
bitmap.bmiHeader.biSize = sizeof(bitmap.bmiHeader);
bitmap.bmiHeader.biWidth = 280;
bitmap.bmiHeader.biHeight = height_to_check;
bitmap.bmiHeader.biPlanes = 1;
bitmap.bmiHeader.biBitCount = 32;
bitmap.bmiHeader.biCompression = BI_RGB;
bitmap.bmiHeader.biSizeImage = 280 * 4 * height_to_check;
bitmap.bmiHeader.biClrUsed = 0;
bitmap.bmiHeader.biClrImportant = 0;
HBITMAP hBitmap2 = CreateDIBSection(hdcTemp, &bitmap, DIB_RGB_COLORS, (void**)(&bitPointer), NULL, NULL);
change to
BYTE* bitPointer;
HDC hdcScreen = GetDC(NULL);
static BITMAPINFO bitmap;
bitmap.bmiHeader.biSize = sizeof(bitmap.bmiHeader);
bitmap.bmiHeader.biWidth = 280;
bitmap.bmiHeader.biHeight = height_to_check;
bitmap.bmiHeader.biPlanes = 1;
bitmap.bmiHeader.biBitCount = 32;
bitmap.bmiHeader.biCompression = BI_RGB;
bitmap.bmiHeader.biSizeImage = 280 * 4 * height_to_check;
bitmap.bmiHeader.biClrUsed = 0;
bitmap.bmiHeader.biClrImportant = 0;
HBITMAP hBitmap2 = CreateDIBSection(hdcScreen, &bitmap, DIB_RGB_COLORS, (void**)(&bitPointer), NULL, NULL);
ReleaseDC(NULL, hdcScreen);
i.e. don't pass an HDC returned from CreateCompatibleDC to CreateDIBSection; use the device context of the screen.
Calling CreateCompatibleDC creates the device context with a 1x1 monochrome bitmap associated with it no matter what HDC you passed in so when you create a bitmap or DIB section compatible to that HDC Win32 tries to be compatible with the monochrome bitmap which you dont want.
At present, MSDN points out that "one or more input parameters are invalid" is reasonable. Tests show that if the bitmap size is too large or the input value is invalid, such as zero, NULL will be returned.

Error setting class attribute with pointer

I am having an issue setting class attributes with a pointer, here is what my code looks like: (this version will compiles, but you need the have minesweeper window open).
WindowObject.h:
#ifndef WINDOWOBJECT_H_INCLUDED
#define WINDOWOBJECT_H_INCLUDED
#include <windows.h>
#include <stdio.h>
class WindowObject
{
public:
WindowObject();
BYTE* getPixelMap() const;
void setPixelMap(BYTE* bitPointer);
void getPixelMap(HWND hwnd);
private:
BYTE* mPixelMap;
};
#endif
WindowObject.cpp:
#include "WindowObject.h"
WindowObject::WindowObject()
{
}
BYTE* WindowObject::getPixelMap() const
{
printf("getPixelMap: %d\n", mPixelMap[0]); // it crashes, bad pointer!!
return mPixelMap;
}
void WindowObject::setPixelMap(BYTE *bitPointer)
{
printf("setPixelMap 1: %d\n", bitPointer[0]); // It works!
mPixelMap = bitPointer;
printf("setPixelMap 2: %d\n", mPixelMap [0]); // Why does this works and getPixelMap doesn't???
}
void WindowObject::getPixelMap(HWND hwnd)
{
HDC hdcWindow, hdcMemDC = NULL;
HBITMAP hbmScreen = NULL;
BITMAP bmpScreen;
// Retrieve the handle to a display device context for the client
// area of the window.
hdcWindow = GetDC(hwnd);
// Create a compatible DC which is used in a BitBlt from the window DC
hdcMemDC = CreateCompatibleDC(hdcWindow);
if(!hdcMemDC)
{
MessageBoxA(hwnd, "CreateCompatibleDC has failed", "Failed", MB_OK);
goto done;
}
// Get the client area for size calculation
RECT rcClient;
GetClientRect(hwnd, &rcClient);
// Create a compatible bitmap from the Window DC
hbmScreen = CreateCompatibleBitmap(hdcWindow, rcClient.right-rcClient.left, rcClient.bottom-rcClient.top);
if(!hbmScreen)
{
MessageBoxA(hwnd, "CreateCompatibleBitmap Failed", "Failed", MB_OK);
goto done;
}
//Get the BITMAP from the HBITMAP
GetObject(hbmScreen,sizeof(BITMAP),&bmpScreen);
BITMAPFILEHEADER bmfHeader;
BITMAPINFOHEADER bi;
BYTE* bitPointer;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bmpScreen.bmWidth;
bi.biHeight = bmpScreen.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
// Create a device independent bitmap that so that we can write to it directly
hbmScreen = CreateDIBSection(hdcMemDC, (BITMAPINFO *)&bi, DIB_RGB_COLORS, (void**)(&bitPointer), NULL, NULL);
// Select the compatible bitmap into the compatible memory DC.
SelectObject(hdcMemDC, hbmScreen);
// Bit block transfer into our compatible memory DC.
if ( !BitBlt(hdcMemDC, 0, 0, rcClient.right-rcClient.left, rcClient.bottom-rcClient.top, hdcWindow, 0, 0, SRCCOPY) )
{
MessageBox(hwnd, "BitBlt has failed", "Failed", MB_OK);
goto done;
}
// Create PixelMap
int width = (rcClient.right-rcClient.left),
height = (rcClient.bottom-rcClient.top);
printf("Inside getPixelMap: %d\n", bitPointer[0]); // it works!
setPixelMap(bitPointer);
done:
DeleteObject(hbmScreen);
DeleteObject(hdcMemDC);
ReleaseDC(hwnd, hdcWindow);
}
main.cpp:
#include "WindowObject.h"
int main()
{
WindowObject wObj;
HWND hwnd;
hwnd = FindWindow(NULL, "Minesweeper");
if (hwnd)
{
wObj.getPixelMap(hwnd);
BYTE* bPointer = wObj.getPixelMap();
printf("After getPixelMap: %d", bPointer[0]);
}
}
The program crashes, and it gives me an "access violation reading location". I'm almost 100% sure the problem comes from my pointer but I find it wierd that it works in every stage of my tests, except when I get it with my getPixelMap method. Any help would be much appreciated!
The problem is that this data member either is not initialized (if an object of the class is default initialized) or has value 0. So using member function getPixelMap() is unsafe. Also as I think the life of an argument of function setPixelMap may be less than the life of an object of the class. So the behaviour of the program is undefined.
I think it would be better to use either a smart pointer or to allocate a memory for using by the data member yourself.
Inside the function getPixelMap(HWND hwnd) you are calling DeleteObject on the returned bitmap, after saving the pointer but before returning. So by the time you access the bitmap later, it is no longer a valid pointer, since according to the CreateDIBSection docs it says that the bitmap will be deallocated when you call DeleteObject.
You will need to keep the bitmap object (hbmScreen) and delete it later, perhaps in the destructor.

Reset existing HBITMAP as desktop background (Win32)

I wish to create a transparent window over the desktop.
For that purpose I've created an HDC with a background of the desktop (created HBITMAP of the desktop and applied it to my HDC), and invoked UpdateLayeredWindow.
So far, so good.
for performance issues i need to keep a persistent GDI+ object, meaning my HDC and HBITMAP need to stay the same handles between paintings (assuming the desktop DC didn't change), same as in this question.
In the first painting iteration all goes well. in the second painting iteration, since the HDC and HBITMAP haven't changed, I repaint on the existing HDC, meaning i get double images (the background is not erased).
Here's a code example of what I'm doing:
bool SomeUI::Draw()
{
BLENDFUNCTION blend = {0};
POINT ptPos = {0};
SIZE sizeWnd = {0};
POINT ptSrc = {0};
BOOL bUpdate = FALSE;
// Get the client rect
RECT rctWindow;
bool bGot = GetWindowRect(rctWindow);
if (!bGot)
return false;
// Get the desktop's device context
HDC hDCDesktop = GetDC(NULL);
if (!hDCDesktop)
return false;
int nWidth = abs(rctWindow.right - rctWindow.left);
int nHeight = abs(rctWindow.bottom - rctWindow.top);
// Create 32Bit bitmap to apply PNG transparency
VOID *ppvBits = NULL;
BITMAPINFO BitmapInfo = {0};
BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BitmapInfo.bmiHeader.biWidth = nWidth;
BitmapInfo.bmiHeader.biHeight = nHeight;
BitmapInfo.bmiHeader.biPlanes = 1;
BitmapInfo.bmiHeader.biBitCount = 32;
BitmapInfo.bmiHeader.biCompression = BI_RGB;
HBITMAP hBmp = CreateDIBSection(hDCDesktop, &BitmapInfo, DIB_RGB_COLORS, &ppvBits, NULL, 0);
if (!hBmp || hBmp==(HBITMAP)ERROR_INVALID_PARAMETER)
goto releaseHandles;
// Create a compatible DC and select the newly created bitmap
if (!m_hDC)
{
m_hDC = CreateCompatibleDC(hDCDesktop);
if (!m_hDC)
goto releaseHandles;
SelectObject(m_hDC, hBmp);
}
else
{
///////////////////////////////////////////////////////////////////////
//
// The problem lies here, this is where I need to reset the HBITMAP
// according to the desktop here (to have a transparent DC to work on)
//
///////////////////////////////////////////////////////////////////////
}
// The drawing logic
bool bInnerDraw = Draw(m_hDC);
if (!bInnerDraw)
goto releaseHandles;
// Call UpdateLayeredWindow
blend.BlendOp = AC_SRC_OVER;
blend.SourceConstantAlpha = 255;
blend.AlphaFormat = AC_SRC_ALPHA;
sizeWnd.cx = nWidth;
sizeWnd.cy = nHeight;
ptPos.x = rctWindow.left;
ptPos.y = rctWindow.top;
bUpdate = UpdateLayeredWindow(m_hWnd, hDCDesktop, &ptPos, &sizeWnd, m_hDC, &ptSrc, 0, &blend, ULW_ALPHA);
if (!bUpdate)
goto releaseHandles;
releaseHandles:
// releasing handles
}
Any ideas?
Found the Answer:
In order to reset the persistent HBITMAP, (reminder: it needs to stay the same handle), we'll set the desktop background of that area to a temporary HBITMAP and copy it to the persistent HBITMAP.
To achieve that (copying from one HBITMAP to the other), We'll create a temporary HDC and select the temporary HBITMAP to it, and copy the temporary HDC to the persistent HDC, usint BitBlt
Here's the code:
hBmpTemp = CreateDIBSection(hDCDesktop, &BitmapInfo, DIB_RGB_COLORS, &ppvBits, NULL, 0);
if (!hBmpTemp || hBmpTemp==(HBITMAP)ERROR_INVALID_PARAMETER)
goto releaseHandles;
HDC hTempDC = CreateCompatibleDC(NULL);
if (!hTempDC)
goto releaseHandles;
SelectObject(hTempDC, hBmpTemp);
::BitBlt(m_hPersistentDC, 0, 0, nWidth, nHeight, hTempDC, rctWindow.left, rctWindow.top, SRCCOPY);
::DeleteDC(hTempDC);