CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2001
    Posts
    2,455

    I am corrupting memory ?

    I creat an edit box dynamically to allow the user to modify text. The user types in the information and upon pressing enter twice, the edit box sends a message to the view that the edit is complete. The OnEditComplete(...) handler then obtains information from the edit box save the information to the database and hides itself. The edit box is not destroyed until it is re-created or the view is closed in which the destructor deletes this.

    There has to be some memory corruption going on because when I open a schedule in my application close and reopen, everything is fine until I create this edit box, make some changes and press enter twice.

    Code:
    void CMileStone::OnLButtonDblClk(UINT nFlags, CPoint point) 
    {
    	CClientDC dc(this);
    	OnPrepareDC(&dc);
    	
    	CRect rcBoundary = GetCalendar()->GetBoundary();
    	dc.LPtoDP(&rcBoundary);
    	rcBoundary.NormalizeRect();
    
    	if(rcBoundary.PtInRect(point))
    	{
    		CRect rcWorkspace = GetCalendar()->GetWorkspaceRect();
    		dc.LPtoDP(rcWorkspace);
    		rcWorkspace.NormalizeRect();
    
    		if(rcWorkspace.PtInRect(point))
    		{
    		}
    		else
    		{
    			dc.DPtoLP(&rcWorkspace);
    			CPoint pt(point);
    			dc.DPtoLP(&pt);
    
    			CMileStoneRowLabel* pLabel = NULL;
    			CMileStoneRow* pRow = NULL;
    
    			if(GetRowByPoint(pt, pRow) >= 0)
    			{
    				m_nCurrentRowID = pRow->GetRowID();
    				if(pRow->GetRowLabelByPoint(pt, pLabel))
    				{
    					CRect rc = pLabel->GetBoundary();
    					dc.LPtoDP(&rc);
    					rc.NormalizeRect();
    					rc.DeflateRect(CSize(2,2));
    					
    					if(m_pEdit)
    						delete m_pEdit;
    
    					m_pEdit = new CInPlaceEdit();
    					if(!m_pEdit->CreateInplaceEdit(this, pLabel->GetTextAlignment(), pLabel->GetFontSettings()))
    						return;
    		
    					m_pEdit->SetWindowText(pLabel->GetLabel());
    					m_pEdit->SetExtendedData(0, (DWORD)pLabel);
    					m_pEdit->MoveWindow(&rc);
    					m_pEdit->ShowWindow(SW_SHOW);
    					m_pEdit->SetFocus();
    					m_pEdit->SetSel(0, -1);				}
    			}
    			else
    				m_nCurrentRowID = 0;
    		}
    	}
    
    	CAutoScrollView::OnLButtonDblClk(nFlags, point);
    }
    
    void CMileStone::OnEditComplete(WPARAM wParam, LPARAM lParam)
    {
    	DWORD dwDummy = 0;
    	DWORD dwLabel = 0;
    	m_pEdit->GetExtendedData(dwDummy, dwLabel);
    
    	CMileStoneRowLabel* pLabel = (CMileStoneRowLabel*)dwLabel;
    	if(pLabel != NULL)
    	{
    		int nRowID = pLabel->GetRowID();
    		int nColumnID = pLabel->GetHeadingID();
    		CString csText = m_pEdit->GetText();
    		
    		if(SaveRowLabel(nRowID, nColumnID, pLabel->GetFontSettings(), pLabel->GetTextAlignment(), csText))
    			pLabel->SetLabel(csText);
    	}
    
    	m_pEdit->ShowWindow(SW_HIDE);
    }
    Anyone see anything wrong with the above approach?

    Mike B

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757

    Re: I am corrupting memory ?

    dynamical allocating is unnecessary

    Kuphryn

  3. #3
    Join Date
    Feb 2001
    Posts
    2,455

    Re: I am corrupting memory ?

    Ok, I changed the code in the CInPlaceEdit class to post the message rather than send the message, this allowed me to delete the edit right away in OnEditComplete(), but I still have the problem.

    Code:
    void CInPlaceEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
    	if(nChar == VK_RETURN)
    	{
    		m_nEnterPressed++;
    		if(m_nEnterPressed == 2)
    			GetParent()->PostMessage(TEXT_EDIT_COMPLETE, 0, 0);	}
    	else
    	{
    		m_nEnterPressed--;
    		if(m_nEnterPressed < 0)
    			m_nEnterPressed = 0;
    	}
    	CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
    }
    
    void CMileStone::OnEditComplete(WPARAM wParam, LPARAM lParam)
    {
    	DWORD dwDummy = 0;
    	DWORD dwLabel = 0;
    	m_pEdit->GetExtendedData(dwDummy, dwLabel);
    
    	CMileStoneRowLabel* pLabel = (CMileStoneRowLabel*)dwLabel;
    	if(pLabel != NULL)
    	{
    		int nRowID = pLabel->GetRowID();
    		int nColumnID = pLabel->GetHeadingID();
    		CString csText = m_pEdit->GetText();
    		
    		if(SaveRowLabel(nRowID, nColumnID, pLabel->GetFontSettings(), pLabel->GetTextAlignment(), csText))
    			pLabel->SetLabel(csText);
    	}
    
    	m_pEdit->ShowWindow(SW_HIDE);
    	delete m_pEdit;
    	m_pEdit = NULL;
    }
    Also, one other question, the same logfont settings are used by the edit box and the label drawn in the view, but they appear to be much different in size. Is there something with the edit box that doesn't allow the font height to change?

    Mike B

  4. #4
    Join Date
    Feb 2001
    Posts
    2,455

    Re: I am corrupting memory ?

    Quote Originally Posted by kuphryn
    dynamical allocating is unnecessary

    Kuphryn
    Not sure how I can do it any other way. I was under the impression that certain settings cannot be set for the CEdit control during runtime, such as text alignment.

    1) If I create the edit on the stack, it just goes out of scope.
    2) If I create a member CInPlaceEdit m_edit;, I cannot change the text alignment.

    Unless I am missing something, I need to dynamically create the edit. Do you have any suggestions?

    Mike B

  5. #5
    Join Date
    Feb 2002
    Posts
    3,788

    Re: I am corrupting memory ?

    why are you using pointers?

  6. #6
    Join Date
    Feb 2001
    Posts
    2,455

    Re: I am corrupting memory ?

    Quote Originally Posted by Alin
    why are you using pointers?
    OK, this is for you and kuphryn, I have changed the code, I am no longer using pointers.

    I created a CInPlaceEdit as a member variable of the class. I then call its CreateInPlaceEdit(...) function which creates the control, I then set everything and show the edit. Then in OnEditComplete() I call Detach() on the edit (Is that all I have to do?).

    Code:
    void CMileStone::OnLButtonDblClk(UINT nFlags, CPoint point) 
    {
       // A buch of irrelevant code
       					
    					if(!m_edit.CreateInplaceEdit(this, pLabel->GetTextAlignment(), pLabel->GetFontSettings()))
    						return;
    		
    					m_edit.SetWindowText(pLabel->GetLabel());
    					m_edit.SetExtendedData(0, (DWORD)pLabel);
    					m_edit.MoveWindow(&rc);
    					m_edit.ShowWindow(SW_SHOW);
    					m_edit.SetFocus();
    					m_edit.SetSel(0, -1);
    				}
    			}
    			else
    				m_nCurrentRowID = 0;
    		}
    	}
    
    	CAutoScrollView::OnLButtonDblClk(nFlags, point);
    }
    
    void CMileStone::OnEditComplete(WPARAM wParam, LPARAM lParam)
    {
    	DWORD dwDummy = 0;
    	DWORD dwLabel = 0;
    	m_edit.GetExtendedData(dwDummy, dwLabel);
    
    
    	CMileStoneRowLabel* pLabel = (CMileStoneRowLabel*)dwLabel;
    	if(pLabel != NULL)
    	{
    		int nRowID = pLabel->GetRowID();
    		int nColumnID = pLabel->GetHeadingID();
    		CString csText = m_edit.GetText();
    		
    		if(SaveRowLabel(nRowID, nColumnID, pLabel->GetFontSettings(), pLabel->GetTextAlignment(), csText))
    			pLabel->SetLabel(csText);
    	}
    
    	m_edit.ShowWindow(SW_HIDE);
    	m_edit.Detach();
    /*	delete m_pEdit;
    	m_pEdit = NULL;*/
    }
    I am now not convinced it is the edit causing the problem, might be the CMileStoneItemLabel* that is being passed through the edit box, CInPlaceEdit::SetExtendedData(DWORD dw1, DWORD dw2) and
    CInPlaceEdit::GetExtendedData(DWORD& dw1, DWORD& dw2).

    Ok, let me look into this a bit further, but answer me this, is Detach() all I have to call on the CInPlaceEdit member in order to create a new one?

    Mike B

  7. #7
    Join Date
    Feb 2002
    Posts
    3,788

    Re: I am corrupting memory ?

    i'd do something like:
    Code:
    if (!IsWindow(m_edit.m_hWnd)) 		
    	 {
    		if(!m_edit.CreateInplaceEdit(this, pLabel->GetTextAlignment(), pLabel->GetFontSettings()))

  8. #8
    Join Date
    Feb 2002
    Posts
    3,788

    Re: I am corrupting memory ?

    Code:
    CaDlg::CaDlg(CWnd* pParent /*=NULL*/)
    	: CDialog(CaDlg::IDD, pParent)
    	, m_bIsHidden(FALSE)
    {
    	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }
    
    void CaDlg::OnCreateShow()
    {
    	// TODO: Add your control notification handler code here
    
    	if(!IsWindow(m_edit.m_hWnd))
    	{
    		if(m_edit.Create(WS_CHILD | WS_BORDER | WS_VISIBLE, CRect(10, 10, 100, 100), this, 1))
    			m_edit.ShowWindow(SW_SHOW);
    	}
    	else
    		if(m_bIsHidden)
    		{
    			m_bIsHidden = FALSE;
    			m_edit.ShowWindow(SW_SHOW);
    		}
    
    }
    
    void CaDlg::OnHide()
    {
    	// TODO: Add your control notification handler code here
    
    	m_edit.ShowWindow(SW_HIDE);
    	m_bIsHidden = TRUE;
    }

  9. #9
    Join Date
    Feb 2002
    Posts
    3,788

    Re: I am corrupting memory ?

    Code:
    void CaDlg::OnCreateShow()
    {
    	// TODO: Add your control notification handler code here
    
    	static BOOL m_bIsCreated = FALSE;
    	
    	if(m_bIsCreated == FALSE)
    	{
    		if(m_edit.Create(WS_CHILD | WS_BORDER | WS_VISIBLE, CRect(10, 10, 100, 100), this, 1))
    		{
    			m_bIsCreated = TRUE;
    			m_edit.ShowWindow(SW_SHOW);
    		}
    		else
    		{	
    			m_bIsCreated = FALSE;
    			return;
    		}
    	}
    	else
    		m_edit.ShowWindow(SW_SHOW);
    }
    
    void CaDlg::OnHide()
    {
    	// TODO: Add your control notification handler code here
    
    	m_edit.ShowWindow(SW_HIDE);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured