473,395 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to freeup the memory...

Hye,
I am using one object which allocates around 2 MB of memory.. Now after
use, I want it to free. But the task can not be done. the memory is still
allocated and not freed. I am using it in thread. so, next time when thread
runs, whole block is allocated for the second time and in just 10 tries, my
PC got hang because of memory problem. I know GlobalFree(HGLOBAL) function
and already used it but in vain.. So, is there any function that I can run
after one turn of my thread (after which I don't want that memory at all.)
So, if the whole area is deleted then also it will be OK.

--
Jigar Mehta
jb*******@yahoo.co.in
Nov 17 '05 #1
4 1664
"Jigar Mehta" <jb*******@yahoo.co.in> wrote in message
news:uK**************@TK2MSFTNGP11.phx.gbl...
Hye,
I am using one object which allocates around 2 MB of memory.. Now after use, I want it to free. But the task can not be done. the memory is still
allocated and not freed. I am using it in thread. so, next time when thread runs, whole block is allocated for the second time and in just 10 tries, my PC got hang because of memory problem. I know GlobalFree(HGLOBAL) function
and already used it but in vain.. So, is there any function that I can run
after one turn of my thread (after which I don't want that memory at all.)
So, if the whole area is deleted then also it will be OK.


You're saying here that you're using GlobalAlloc and a corresponding
GlobalFree doesn't? That just doesn't sound right to me. What does
GlobalFree return? If not NULL, what does GetLastError return?
--
Jeff Partch [VC++ MVP]
Nov 17 '05 #2
Actually I am using functions of WinCap, (MSDN Sample) in that everytime I
capture desktop, I lose 3 MB of memory...

Can you help me after seeing the WinCap & testing it!!...

"Jeff Partch [MVP]" <je***@mvps.org> wrote in message
news:#s**************@TK2MSFTNGP11.phx.gbl...
"Jigar Mehta" <jb*******@yahoo.co.in> wrote in message
news:uK**************@TK2MSFTNGP11.phx.gbl...
Hye,
I am using one object which allocates around 2 MB of memory.. Now after use, I want it to free. But the task can not be done. the memory is still
allocated and not freed. I am using it in thread. so, next time when thread runs, whole block is allocated for the second time and in just 10 tries, my PC got hang because of memory problem. I know GlobalFree(HGLOBAL) function
and already used it but in vain.. So, is there any function that I can run
after one turn of my thread (after which I don't want that memory at all.)
So, if the whole area is deleted then also it will be OK.


You're saying here that you're using GlobalAlloc and a corresponding
GlobalFree doesn't? That just doesn't sound right to me. What does
GlobalFree return? If not NULL, what does GetLastError return?
--
Jeff Partch [VC++ MVP]


Nov 17 '05 #3
Hye Jeff,

Sorry If you are confused from my questions... Actually What I am doing
is, Capturing window into a bitmap file, saving it to BMP (with RLE8
compression), so for saving it to disk, I am creating a hell lot of
variables to handle all the complexity. Now, as you told, my GlobalFree
returns NULL, so, that object is freedup, but the problem is for other
objects of structures and variables that I created to save it to disk. How
to free that.. e.g. here is a snippet of code...
----------------------------------------------------------------------------
-------------
//code starts here... (it is a small function that allocates memory for DIB)
----------------------------------------------------------------------------
-------------
HANDLE CDV_ClientDlg::AllocRoomForDIB(BITMAPINFOHEADER bi, HBITMAP hBitmap,
HWND hwnd)
{
// Figure out the size needed to hold the BITMAPINFO structure
// (which includes the BITMAPINFOHEADER and the color table).
dwLen = bi.biSize + PaletteSize((LPSTR) &bi);
hDIB = GlobalAlloc(GHND,dwLen);
// Check that DIB handle is valid
if (!hDIB)
return NULL;
// Set up the BITMAPINFOHEADER in the newly allocated global memory,
// then call GetDIBits() with lpBits = NULL to have it fill in the
// biSizeImage field for us.
lpbi_2 = (LPBITMAPINFOHEADER)GlobalLock(hDIB);
*lpbi_2 = bi;
hDC_1 = CreateDC ( TEXT("DISPLAY"), NULL, NULL, NULL );
GetDIBits(hDC_1, hBitmap, 0, (UINT) bi.biHeight, NULL, (LPBITMAPINFO)lpbi_2,
DIB_RGB_COLORS);
::ReleaseDC(hwnd, hDC_1);
// If the driver did not fill in the biSizeImage field,
// fill it in -- NOTE: this is a bug in the driver!
if (lpbi_2->biSizeImage == 0)
lpbi_2->biSizeImage = WIDTHBYTES((DWORD)lpbi_2->biWidth *
lpbi_2->biBitCount) * lpbi_2->biHeight;
// Get the size of the memory block we need
dwLen = lpbi_2->biSize + PaletteSize((LPSTR) &bi) + lpbi_2->biSizeImage;
// Unlock the memory block
GlobalUnlock(hDIB);
// ReAlloc the buffer big enough to hold all the bits
if (hTemp = GlobalReAlloc(hDIB,dwLen,0))
return hTemp;
else
{
// Else free memory block and return failure
GlobalFree(hDIB);
return NULL;
}
}
HANDLE CDV_ClientDlg::AllocRoomForDIB(BITMAPINFOHEADER bi, HBITMAP hBitmap,
HWND hwnd)
{
// Figure out the size needed to hold the BITMAPINFO structure
// (which includes the BITMAPINFOHEADER and the color table).
dwLen = bi.biSize + PaletteSize((LPSTR) &bi);
hDIB = GlobalAlloc(GHND,dwLen);
// Check that DIB handle is valid
if (!hDIB)
return NULL;
// Set up the BITMAPINFOHEADER in the newly allocated global memory,
// then call GetDIBits() with lpBits = NULL to have it fill in the
// biSizeImage field for us.
lpbi_2 = (LPBITMAPINFOHEADER)GlobalLock(hDIB);
*lpbi_2 = bi;
hDC_1 = CreateDC ( TEXT("DISPLAY"), NULL, NULL, NULL );
GetDIBits(hDC_1, hBitmap, 0, (UINT) bi.biHeight, NULL, (LPBITMAPINFO)lpbi_2,
DIB_RGB_COLORS);
::ReleaseDC(hwnd, hDC_1);
// If the driver did not fill in the biSizeImage field,
// fill it in -- NOTE: this is a bug in the driver!
if (lpbi_2->biSizeImage == 0)
lpbi_2->biSizeImage = WIDTHBYTES((DWORD)lpbi_2->biWidth *
lpbi_2->biBitCount) * lpbi_2->biHeight;
// Get the size of the memory block we need
dwLen = lpbi_2->biSize + PaletteSize((LPSTR) &bi) + lpbi_2->biSizeImage;
// Unlock the memory block
GlobalUnlock(hDIB);
// ReAlloc the buffer big enough to hold all the bits
if (hTemp = GlobalReAlloc(hDIB,dwLen,0))
return hTemp;
else
{
// Else free memory block and return failure
GlobalFree(hDIB);
return NULL;
}
}
----------------------------------------------------------------------------
-------------
//code ends here
----------------------------------------------------------------------------
-------------
Now here you can see that I am allocating some memory, after that I Save the
bitmap and then I want to free these memory.. All variables used are global
so, I will be able to free them up later.. But free() method gives me error
for lpbi_2 i.e. free(lpbi_2) gives runtime error (that abort dialog)...

If you are still confused and want a full application code, (want to help me
really), Please tell me, I will send you full application (of VC.NET). I am
tired of this as I am trying to do this from last 4 hours.)
Nov 17 '05 #4
"Jigar Mehta" <jb*******@yahoo.co.in> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Now here you can see that I am allocating some memory, after that I Save the bitmap and then I want to free these memory.. All variables used are global so, I will be able to free them up later.. But free() method gives me error for lpbi_2 i.e. free(lpbi_2) gives runtime error (that abort dialog)...


Hi,

The only objects I see that need to be 'freed' are the HDC, and the HGLOBALs
hDib and hTemp. You obtain the HDC using CreateDC but then call ReleaseDC on
it. Rather you should use DeleteDC, I think. Otherwise, hDib seems local to
the function and seems properly managed to me. The hTemp is a return value,
so it is the caller's responsibility to call GlobalFree on it when it is
done. Nothing really seems to warrant a global or member variable (although
dwLen might be handy for future reference). You mention that using free is
failing -- as well it should. Nothing in the above needs to be passed to
free. Certainly not lpbi_2, that is not something that needs to deallocated
at all.

FWIW, I wrote the thing like so...

HANDLE AllocRoomForDIB(VOID)
{
DWORD dwLen = 1000;
HGLOBAL hDIB = GlobalAlloc(GHND,dwLen);
LPBITMAPINFOHEADER lpbi_2;
HGLOBAL hTemp;
// Check that DIB handle is valid
if (!hDIB)
return NULL;
lpbi_2 = (LPBITMAPINFOHEADER)GlobalLock(hDIB);
GlobalUnlock(hDIB);
dwLen *= 3000;
hTemp = GlobalReAlloc(hDIB,dwLen,0);
if (hTemp)
return hTemp;
else
{
// Else free memory block and return failure
OutputDebugString(_T("Freeing in ERROR\n"));
if ((BOOL)GlobalFree(hDIB))
OutputDebugString(_T("\tFAILURE to free\n"));
}
return NULL;
}

....and called it like so...

INT
WINAPI
_tWinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpszCmdLine,
INT nCmdChow
)
{
INT i;
for (i = 0; i < 1000; i++)
{
HANDLE hRet = AllocRoomForDIB();
if (hRet)
{
OutputDebugString(_T("Freeing in CALLER\n"));
if ((BOOL)GlobalFree(hRet))
OutputDebugString(_T("\tFAILURE to free\n"));
}
Sleep(100);
}
return 0;

// Unused
hInstance;
hPrevInstance;
lpszCmdLine;
nCmdChow;
}

....and it never seemed to report either failure or error.

--
Jeff Partch [VC++ MVP]
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since...
4
by: Frank Esser | last post by:
I am using SQL 8 Personal edition with sp2 applied. I set the max server memory to 32MB and leave the min server memory at 0. When my application starts hitting the database hard the memory usage...
32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
4
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on...
9
by: Mike P | last post by:
I know everything about reference counting and making sure you don't have large objects lying around. I have also profiled my app with multiple tools. I know about the fact GC collects memory but...
12
by: Jeremy | last post by:
Hi all, I'm getting very confused about how DB2 uses shared memory and I wonder if someone could clarify matters for me, please ? We are running 32bit DB2 V7.2 FP9 under AIX 4.3.3 on a machine...
22
by: xixi | last post by:
hi, we are using db2 udb v8.1 for windows, i have changed the buffer pool size to accommadate better performance, say size 200000, if i have multiple connection to the same database from...
5
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS...
1
by: Jean-Paul Calderone | last post by:
On Tue, 22 Apr 2008 14:54:37 -0700 (PDT), yzghan@gmail.com wrote: The test doesn't demonstrate any leaks. It does demonstrate that memory usage can remain at or near peak memory usage even after...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.