Thx for your reply. I did try GlobalFree(hGlobal), follow the code of "pstm->Release()", but Purity told me I am trying to release an unallocated memory. Where shd I put the GlobalFree() function in this case? If I put it before pstm->Release(), Purify told me this message : "[W] PAR: Global/LocalLock(0x571001c) arg #1 (hMem) attempt to lock handle that is on pending free queue. {1 occurrence}" Thx again Vincent
Vincent Ye
Posts
-
Need help for memory leak -
Need help for memory leakHi All, I use the code below to load a bitmap file to my program. void CStartupDlg::LoadPictureFile(HDC hdc, LPCTSTR szFile, CBitmap *pBitmap, CSize &mSize) { // open file HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); _ASSERTE(INVALID_HANDLE_VALUE != hFile); // get file size DWORD dwFileSize = GetFileSize(hFile, NULL); _ASSERTE(-1 != dwFileSize); LPVOID pvData = NULL; // alloc memory based on file size HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize); _ASSERTE(NULL != hGlobal); pvData = GlobalLock(hGlobal); _ASSERTE(NULL != pvData); DWORD dwBytesRead = 0; // read file and store in global memory BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL); _ASSERTE(FALSE != bRead); GlobalUnlock(hGlobal); CloseHandle(hFile); // create IStream* from global memory LPSTREAM pstm; HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm); _ASSERTE(SUCCEEDED(hr) && pstm); // Create IPicture from image file hr = ::OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&gpPicture); <==== It said the memory allocated here is not released. _ASSERTE(SUCCEEDED(hr) && gpPicture); pstm->Release(); ........................................................................... gpPicture->Release(); } After I use "Rational Purify" to check memory leak, it reports a memory leak happened. Could you tell me what I shd release? Thanks in advance Vincent
-
A SetFont problemThank you for your information. However it is not that case. Below is the answer from MS newsgroup: " There is no default implementation of WM_SETFONT and WM_GETFONT. You need to implement these message handlers to store and return the current HFONT (or NULL as the case may be), and Invalidate if LPARAM is TRUE. IIRC, ClassWizard won't help with these, so you'll need to add them manually using ON_MESSAGE macros in your message-map, and remember that the prototype is 'LRESULT memberFxn(WPARAM, LPARAM)'. In your WM_PAINT handler, you will need to SelectObject the currently stored HFONT (if there is one) into your CPaintDC, and SelectObject it back out again when you're done. -- Jeff Partch [VC++ MVP] " I follow his instruction. It works well. Anyway, thanks again.
-
A SetFont problemSure I did. Some of my code is below: void CColItem::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CRect rect; GetClientRect(rect); CFont *OldFont = NULL; OldFont = dc.SelectObject(GetFont()); .............. dc.TextOut(xText, yText, strWindowText); .............. dc.SelectObject(OldFont); } Thanks again
-
A SetFont problemThank you for your reply. After reading your comment, I have another question. I have used SetFont to set a font to it. How can I know which font I have set when I want to use it in Paint function. Because my class is used by other people, I have to use a generic way to know which font they set. Actually I have tried another way, I override the SetFont function in my class, and remember the font they said. It worked. But because the SetFont is not a virtual one, when they use a CWnd pointer to call the SetFont function, my SetFont will not be called. Thanks again.
-
A SetFont problemHi all, I have a question about SetFont. Now I create a class that is derived from CWnd, and use SetFont to set a font to it. Then I override the Paint function to display the content I want. The problem happens there, no matter what font I set, when I draw the text in Paint, it display with the same font, I think it is the system font. How can I make the SetFont work? Thanks in advance
-
how to set tab stop order programmaticallyThank you for replying so fast. actually, I want to insert an own control into the dialog. I create this control in OnCreate. I hope the user can use "tab" to go through all the controls. for example, there are two buttons in the dialog, call A and B. now I create an own control, I hope to set its tab stop order between A and B. Then when user presses "tab", the focus will go to A, own control, B, and go back to A. Thanks again.
-
how to set tab stop order programmaticallyHow can I change the tab stop order for the controls in a dialog after the dialog is displayed? Thanks in advance.
-
How to display a string vertically?Dear Alvaro, Thank you very much. After I added "lf.lfOutPrecision = OUT_TT_ONLY_PRECIS;", it worked. Thanks again Vincent
-
How to display a string vertically?When I use TextOut() to display a string, the string is displayed horizontally. So I create a font with lfEscapement & lfOrientation = 900. And select it to the CDC object. I think the string should be displayed vertically. Unfortunately, it isn't displayed vertically. How can I do that? Thanks in advance. Vincent
-
A question about TreeCtrlvoid CFileViewTab::OnRclickTreeWorkspace(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here LRESULT lReturn = 0; CPoint point(GetMessagePos()); ScreenToClient(&point); CTreeCtrl *pTree = (CTreeCtrl *)GetDlgItem(IDC_TREE_WORKSPACE); UINT flag; HTREEITEM hItem; if ((hItem = pTree->HitTest(point, &flag)) != NULL) { if (flag & TVHT_ONITEM) { pTree->SelectItem(hItem); ...... .....
-
A question about TreeCtrlI have a question about TreeCtrl. When I use mouse right click one item in the TreeCtrl, this item is highlight when the right mouse key is down. But when the right mouse key is up, this item loses highlight, the previous highlight item is highlight again. How can I keep the item that I right click highlight? Thanks in advance.
-
How to get text in ComboBoxHi, all I created a Dropdown ComboBox. now I want to handle the CBN_EDITCHANGE message. At that time, how can I get the text in the editbox of the combobox box? I know one way is to use UpdateData, but it will update all the data include other controls, how can I just get the text of the combobox? Thanks in advance
-
How to change editbox's background color when it is set to read-only?I have tried to handle the WM_CTLCOLOR message, but failed. When the editbox is set to read-only, it will not send the WM_CTLCOLOR to its parent window. Anyway, thanks a lot.
-
How to change editbox's background color when it is set to read-only?When I set the editbox to read-only, its backgroud color will be gray. Can I set it to another color? Thanks in advance.
-
ShellExecute and memory footprint?!I suggest you change the first parameter of ShellExecute from "hWnd" to "NULL".
-
How to select items in a ListCtrl programmaticallyThank you very much.
-
How to select items in a ListCtrl programmaticallyThank you very much.
-
How to select items in a ListCtrl programmaticallyHi, All, How can I select item in a ListCtrl programmatically? the ListCtrl is in Report View mode. Thanks in advance
-
How to know the user switch to another MDI child window?I am designing a MDI program. there are many MDI child window. How can I know the user change the activated status from one MDI child window to another? Why do I want to do this? Because when one MDI child window is deactivated, I have to save its data to files. Thanks in advance.