|
Thanks again Phil, I'm pretty sure alignment won't be a problem because malloc handles the correct alignment for buffers depending on their size (it doesn't matter exactly what object they are, only the size of the object will determine whether malloc has to pad the memory with x bytes to align it ). Cheers for that article mind, very helpful explanation of placement new, which is new to me myself. Thanks again for your help Phil, much appreciated,
Alan.
|
|
|
|
|
I'm not sure realloc will be more efficient than doing another new and copying. That's what realloc does beneath the surface, anyway. You only literally 'expand' an allocated space with realloc on those rare occasions where you have not done any other memory allocations since you malloc'd the memory you are expanding.
I'd look into how efficient you can get the STL using custom allocator classes before you go as far as reimplementing a string class.
|
|
|
|
|
I think realloc will be faster when it comes to large objects because it will merely allocate a larger block of memory and copy the contents over - no destructors or constructors are called, therefore everything is A1. Also, new does have extra overhead because of the try,throw,catch implementation. However, using new/delete I'd have to reserve another memory block, but how pray do I move the objects into the new array? If I merely copy the bytes across, when I free up the previous allocation the objects destructor is called and so, those objects in the new array are total garbage (and they will get deleted again the next time a reallocation is made. Nasty. Any ideas on how to get around that would be very useful, and I would definitely use that method instead .
Cheers,
Alan.
|
|
|
|
|
Hi,
I am an amateurish user of VC++. I am developing a program which handles lots of picture files and works with them. The entire operation is carried on a FormView. The picture files are loaded one at a time. I have inserted few functions in the onmousemove() because the requirement was such. Now as the program runs, it goes on gathering memory requirement in a cumulative way (seen through task manager) untill the OS stops the program forcefully. I tried to allocate memories dynamically and freed them whereever the purpose is finished. But mothing worked.
Only one thing drastically brings down the memory, is when the Frame is minimized and again maximized.
What could be the reason for what is happening. Could you give a clue to how to reduce the memory requirement, or where exactly I can free some. The minimization process frees the memories automatically. Does it give some clue?
Thanks in advance
|
|
|
|
|
You have a nasty memory leak! Somewhere in your program, memory is being allocated, but is never being freed, at least in time before your out of memory. Its likely to be something to do with your loading of the pictures, or perhaps drawing of the pictures? Are you using GDI to draw them? If you are make sure you are keeping track of your handles correctly. What are you doing in your minimize/maximize functions that would reduce memory? On maximize, the app has to redraw the the view, which is why I think its something to do with your pictures/drawing of the pictures. Post some code up!
|
|
|
|
|
I'm loading a BMP file with the Attach function of CBitmap and displaying on the formview through the StretchBlt() of CDC.
|
|
|
|
|
OK, Attach takes one param, a handle to an object, how are you getting this handle? Where are you getting this handle? Where are you attaching this handle? When do you destroy this handle? Can you post up your draw code so we can at least see the bigger picture of your drawing process?
|
|
|
|
|
I'm posting it, sorry it must have been a very very clumsy one -
CString filename;
char szFilters[]="Picture Files(*.bmp)|(*.gif)|All Files(*.*)|*.*||";
CFileDialog dlg(TRUE,"bmp","*.bmp",OFN_FILEMUSTEXIST|
OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
szFilters,this);
if(dlg.DoModal()==IDOK)
{
filename=dlg.GetPathName();
}
else return;
OnNewFileLoaded();
bBitmap.Detach();
/*m_Bit.SetBitmap(LoadImage(filename.GetBuffer(20)));*/
bBitmap.Attach((HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE));
// m_Bit.SetBitmap((HBITMAP)bBitmap);
// m_TextDisplay.SetWindowText("Please Click on the Origin of the graph's axes"
// "and Press the DONE button");
CDC* pDC = GetDC();
CBrush brush(pDC->GetPixel(1,1)),*oldbrush;
oldbrush = pDC->SelectObject(&brush);
pDC->FloodFill(7,7,pDC->GetPixel(1,1));
pDC->SelectObject(oldbrush);
BITMAP BM;
if(bBitmap.GetObjectType()==0) {
AfxMessageBox("Invalid File Format.\nLoads only Bitmaps.");
return;
}
bBitmap.GetObject(sizeof(BM),&BM);
CRect rectClient;
GetClientRect(&rectClient);
if(rectClient.Height()<bm.bmheight||rectclient.width()<bm.bmwidth)
{
="" cmainframe*="" pmain="(CMainFrame*)" getparentframe();
="" pmain-="">ActivateFrame(SW_SHOWMAXIMIZED);
AfxGetApp()->m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);
}
if(WIDTH>BM.bmWidth&&HEIGHT>BM.bmHeight){
destWidth = BM.bmWidth;
destHeight = BM.bmHeight;
}
else if(WIDTH>BM.bmWidth&&HEIGHT<bm.bmheight){
destwidth="(int)(destHeight*((double)BM.bmWidth/(double)BM.bmHeight));
" }
="" else="" if(width<bm.bmwidth&&height="">BM.bmHeight){
destHeight = (int)(destWidth*((double)BM.bmHeight/(double)BM.bmWidth));
}
else
{
if(BM.bmWidth>BM.bmHeight){
destWidth = WIDTH;
destHeight = int((double(BM.bmHeight)/double(BM.bmWidth))*double(destWidth));
}
else{
destHeight = HEIGHT;
destWidth = int((double(BM.bmWidth)/double(BM.bmHeight))*double(destHeight));
}
}
CPoint ratio;
ratio.x= destWidth;
ratio.y= destHeight;
ratioHeight = (double)BM.bmHeight/(double)ratio.y;
ratioWidth = (double)BM.bmWidth/(double)ratio.x;
CMainFrame* pMain= (CMainFrame*) GetParentFrame();
CToolBarCtrl* pCtrl = (CToolBarCtrl*)pMain->GetToolBar();
if(pCtrl->IsButtonChecked(ID_TOOLZOOM)){
OnToolzoom();
}
if(m_ShowZOOM.IsWindowVisible()) m_ShowZOOM.ShowWindow(FALSE);
RedrawWindow(NULL,NULL);
if(!rectBitmap.IsRectEmpty())this->ValidateRect(&rectBitmap);
if(HBITMAP(bBitmap)!=NULL){
BITMAP BM;
CDC MemDC;
MemDC.CreateCompatibleDC(NULL);
MemDC.SelectObject(bBitmap);
if(bBitmap.GetObjectType()==0){
AfxMessageBox("Invalid File Format.\nLoads only Bitmaps.");
return;
}
bBitmap.GetObject(sizeof(BM),&BM);
GetDC()->StretchBlt(7,7,destWidth,destHeight,&MemDC,0,0,BM.bmWidth,BM.bmHeight,SRCCOPY);
rectBitmap.top = 7;rectBitmap.left = 7;rectBitmap.bottom = destHeight+7;
rectBitmap.right = destWidth+7;
GetDC()->DPtoLP(&rectBitmap);
|
|
|
|
|
OK, we're starting to get somewhere now. If this is you OnPaint() or whatever draw function, there is no need to be keep loading the picture in. What seems to be happening is LoadImage is loading a handle to a resource each time this function is called, but it is never freed. With bitmaps this is vital because you will soon run out of memory. Detaching the handle is not enough to free the memory allocated to the handle. I would store the result of your LoadImage call as a member HANDLE (alongside bBitmap) in the class that calls this draw function. I would also separate the file loading code so when you click, perhaps a button, the filedialog is called, if the HANDLE is NULL it then simply allocates a new one using LoadImage, otherwise use Detach on bBitmap followed by DeleteObject on the existing HANDLE, before LoadImage on the new pic. This frees your draw code to concentrate on drawing, no monster leaks would occur because everything you use to draw is in memory and isn't changing (only time it changes is when you click THE button and choose a file). This is definitely where A monster leak would be occurring, perhaps it is THE monster leak .
|
|
|
|
|
You can use the features in Visual Studio to detect where memory is eaten. Run the program in debug mode ( press F5 ), and take your program through all the scenarios where there are possiblities of leak. Stop the application, and see the debug tab in output window. There you can see all the memory locations which has not been freed. Double clicking on it will take you to the position in code where that memory is allocated.
Hope this can be of your help.
"A robust program is resistant to errors -- it either works correctly, or it does not work at all; whereas a fault tolerant program must actually recover from errors."
|
|
|
|
|
Thanks Dangleberry, thanks Jagadeesh, yours suggestions were really beneficial, I could identify all the leaks and could mend most of them. Thanks again
|
|
|
|
|
I want to know basic information about socket and it's programming in visual c++. Moreover what is the role of socket programming left in dot net.
|
|
|
|
|
they are still using sockets......but i think they classes are different
|
|
|
|
|
See this URLs :
http://www.dotnet247.com/247reference/articles/0/1537.aspx
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/windows_sockets_2_architecture_2.asp
"A robust program is resistant to errors -- it either works correctly, or it does not work at all; whereas a fault tolerant program must actually recover from errors."
|
|
|
|
|
Hi all,
I am placing a MENU on a Dialog window.
And trying to enable and disable the Menu Items plus putting checkmark and radio, I am overiding the Update<menuitem-name>(... pCmdUI,...) method.
The problem is it not working, I also override the UpdatePopupMenu(...) method of CFrame by using a sample code from MSDN.
Please anybody helps me in this regard.
Dammy More
|
|
|
|
|
Update Command UI Handlers Do Not Work for Menu in dialogs.
You can use ON_WM_INITMENUPOPUP message instead of this.
See this article in msdn
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q242/5/77.asp&NoWebContent=1
"A robust program is resistant to errors -- it either works correctly, or it does not work at all; whereas a fault tolerant program must actually recover from errors."
|
|
|
|
|
Hi,
I have a project I downloaded from cp and dont know why, but when I try to use some classes that are in many (8 0r 9) files, I get this message:
*.cpp(2) : fatal error C1010: unexpected end of file while looking for precompiled header directive *.cpp
I get about 10 of these when I compile. Does anyone know what this means?
Where is a good place to look up compiler and linker errors?
I have had this problem with classes Ive wrote before.
Thanks.
|
|
|
|
|
The best place to lookup errors is in the help.
This means you are attempting to use precompiled headers, but the precompiled header is not present in the files. For Wizard generated files, this is "stdafx.h".
You can turn precompiled headers off for a project in the project settings. For large projects it will slow down the compiles significantly, for small projects, the impact is negligable, though measurable.
|
|
|
|
|
It's also possible to turn precompiled headers off for separates files so the project compilation won't slow down too much. Right click on the .cpp file, choose settings, go in the C/C++ tab, select Precompiled headers category and choose Not using precompiled headers
This is a better solution when you need to disable them only for a few files in a big project
|
|
|
|
|
Joe Woodbury have detailed it.
the same you can include"stdafx.h" to solve it .
dupengfei
|
|
|
|
|
Still confusing,
Goto Project->Settings->C/C++->Precompiled Headers
Select Automatic use of Precompiled Headers.
Thanks.
The Phantom.
|
|
|
|
|
Thanks. I fixed my problem. You say precompiled header files will speed up or slow down compile?
|
|
|
|
|
When the .PCH file is being created, the compile time will take longer. Subsequent compiles will be much shorter. If, however, you have a small project, the difference will not be noticeable.
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
|
|
|
|
|
Very easy, add
#include "stdafx.h"
to the files that give those errors.
Make sure it's the first #include though.
|
|
|
|
|
Hi, i have a question abt displaying doubles. Under _MBCS the following codes work but how do i display the double under Unicode? Thanks!
char *buffer;
int decimal, sign;
buffer = _ecvt( m_Height, 5, &decimal, &sign );
AfxMessageBox(buffer);
or
int decimal, sign;
AfxMessageBox(_ecvt(m_Height, 5, &decimal, &sign);
|
|
|
|
|