473,563 Members | 2,709 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(HGLO BAL) 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 1678
"Jigar Mehta" <jb*******@yaho o.co.in> wrote in message
news:uK******** ******@TK2MSFTN GP11.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(HGLO BAL) 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******** ******@TK2MSFTN GP11.phx.gbl...
"Jigar Mehta" <jb*******@yaho o.co.in> wrote in message
news:uK******** ******@TK2MSFTN GP11.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(HGLO BAL) 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 (BITMAPINFOHEAD ER bi, HBITMAP hBitmap,
HWND hwnd)
{
// Figure out the size needed to hold the BITMAPINFO structure
// (which includes the BITMAPINFOHEADE R and the color table).
dwLen = bi.biSize + PaletteSize((LP STR) &bi);
hDIB = GlobalAlloc(GHN D,dwLen);
// Check that DIB handle is valid
if (!hDIB)
return NULL;
// Set up the BITMAPINFOHEADE R in the newly allocated global memory,
// then call GetDIBits() with lpBits = NULL to have it fill in the
// biSizeImage field for us.
lpbi_2 = (LPBITMAPINFOHE ADER)GlobalLock (hDIB);
*lpbi_2 = bi;
hDC_1 = CreateDC ( TEXT("DISPLAY") , NULL, NULL, NULL );
GetDIBits(hDC_1 , hBitmap, 0, (UINT) bi.biHeight, NULL, (LPBITMAPINFO)l pbi_2,
DIB_RGB_COLORS) ;
::ReleaseDC(hwn d, 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((DWO RD)lpbi_2->biWidth *
lpbi_2->biBitCount) * lpbi_2->biHeight;
// Get the size of the memory block we need
dwLen = lpbi_2->biSize + PaletteSize((LP STR) &bi) + lpbi_2->biSizeImage;
// Unlock the memory block
GlobalUnlock(hD IB);
// ReAlloc the buffer big enough to hold all the bits
if (hTemp = GlobalReAlloc(h DIB,dwLen,0))
return hTemp;
else
{
// Else free memory block and return failure
GlobalFree(hDIB );
return NULL;
}
}
HANDLE CDV_ClientDlg:: AllocRoomForDIB (BITMAPINFOHEAD ER bi, HBITMAP hBitmap,
HWND hwnd)
{
// Figure out the size needed to hold the BITMAPINFO structure
// (which includes the BITMAPINFOHEADE R and the color table).
dwLen = bi.biSize + PaletteSize((LP STR) &bi);
hDIB = GlobalAlloc(GHN D,dwLen);
// Check that DIB handle is valid
if (!hDIB)
return NULL;
// Set up the BITMAPINFOHEADE R in the newly allocated global memory,
// then call GetDIBits() with lpBits = NULL to have it fill in the
// biSizeImage field for us.
lpbi_2 = (LPBITMAPINFOHE ADER)GlobalLock (hDIB);
*lpbi_2 = bi;
hDC_1 = CreateDC ( TEXT("DISPLAY") , NULL, NULL, NULL );
GetDIBits(hDC_1 , hBitmap, 0, (UINT) bi.biHeight, NULL, (LPBITMAPINFO)l pbi_2,
DIB_RGB_COLORS) ;
::ReleaseDC(hwn d, 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((DWO RD)lpbi_2->biWidth *
lpbi_2->biBitCount) * lpbi_2->biHeight;
// Get the size of the memory block we need
dwLen = lpbi_2->biSize + PaletteSize((LP STR) &bi) + lpbi_2->biSizeImage;
// Unlock the memory block
GlobalUnlock(hD IB);
// ReAlloc the buffer big enough to hold all the bits
if (hTemp = GlobalReAlloc(h DIB,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*******@yaho o.co.in> wrote in message
news:%2******** ********@tk2msf tngp13.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(GHN D,dwLen);
LPBITMAPINFOHEA DER lpbi_2;
HGLOBAL hTemp;
// Check that DIB handle is valid
if (!hDIB)
return NULL;
lpbi_2 = (LPBITMAPINFOHE ADER)GlobalLock (hDIB);
GlobalUnlock(hD IB);
dwLen *= 3000;
hTemp = GlobalReAlloc(h DIB,dwLen,0);
if (hTemp)
return hTemp;
else
{
// Else free memory block and return failure
OutputDebugStri ng(_T("Freeing in ERROR\n"));
if ((BOOL)GlobalFr ee(hDIB))
OutputDebugStri ng(_T("\tFAILUR E 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)
{
OutputDebugStri ng(_T("Freeing in CALLER\n"));
if ((BOOL)GlobalFr ee(hRet))
OutputDebugStri ng(_T("\tFAILUR E 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
2037
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 the thread is over a month old, I decided to start a new one with my response. Please see my comments inline.
4
13000
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 reported through task manager peaks between 41-42MB. I've stopped and restarted the MSSQLserver service and checked that the running values are...
32
3806
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 execution. But I do not think it needs so much memory. About 500M memory should be enough. I have following questions about memory leak. (1).If in my...
4
2572
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 Solaries OS? This means that I use new to allocate memory in one Thread and doesn't use delete to release them.
9
2338
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 not necessary give it back to the OS. It seems that .NET win app will only return memory to the OS when the OS is asking for it. But!!! When...
12
5514
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 with 64 Gb of memory with a single non-partitioned database using extended storage and with intra-parallelism enabled. I've been experimenting...
22
3450
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 application server, will each connection take the memory 800M (200000 x 4k = 800 M), so the memory took will be 800M times number of connections, or the...
5
24632
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 (I am new to Windows so not sure). We are currently bouncing the instance to overcome this error. This generally happen at the end of business day...
1
2028
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 the objects for which that memory was allocated are no longer live in the process. This is only a leak if peak memory goes up again each time you...
0
7664
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7638
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6250
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
923
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.