[RESOLVED] memcpy error in MFC
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 16

Thread: [RESOLVED] memcpy error in MFC

Hybrid View

  1. #1
    Join Date
    Nov 2011
    Location
    India
    Posts
    147

    [RESOLVED] memcpy error in MFC

    Hi,

    I load an image(768*256) using a file path(OpenImageFilePath).

    After load an image, I start to read the loaded Image data using GetBits method and plot the same image data(768*256).

    I'm using the memcpy method, for that i'm getting the below error. memmove function also giving the same error message.

    Please clear me.

    File Name : memcopy.asm
    rep movsd ;N - move all of our dwords


    Code for your reference
    Code:
    void CDlg :: FileOpen()
    {
    	CFileException CFileEx;
    	CStdioFile ReadFile;
    
    	// szFilters is a text string that includes two file name filters:
    	TCHAR szFilters[]= _T("Image Files (*.bmp)");
    
    	CFileDialog fileDlg(TRUE, _T("bmp"), _T("*.bmp"),
    
    	OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters);
    
    	fileDlg.m_ofn.lpstrTitle = _T("Offline Images");
    
    	fileDlg.m_pOFN->lpstrInitialDir=_T("C:\\AcceptedImg\\");
    
    	if(fileDlg.DoModal() == IDOK)
    	{
    		OpenImageFilePath = fileDlg.GetPathName();
    		OpenImageFileFlag = true; 
    	}
    
    	//Refresh Window
    	CRect mRect;
    	GetClientRect(&mRect);
    	InvalidateRect(&mRect); 
    }
    
    void CDlg::OnPaint() 
    { 
    	CPaintDC dc(this); // device context for painting 
    	if(OpenImageFileFlag)
    	{ 	//Load Image
    		CDC *pDC; 
    		pDC=GetDC();
    
    		CImage ObjImage ;
    		ObjImage.Load(OpenImageFilePath) ; //Image Path 
    
    		CBitmap pBitmap ;
    		pBitmap.Attach(ObjImage.Detach());
    
    		BITMAP bm;
    		pBitmap.GetObject( sizeof(BITMAP), &bm );	
    
    		CDC dcMem;
    		dcMem.CreateCompatibleDC( pDC );
    
    		CBitmap* pbmpOld = dcMem.SelectObject( &pBitmap );
    		pDC->BitBlt(25,360, bm.bmWidth, bm.bmHeight,&dcMem, 0,0, SRCCOPY );
    
    		dcMem.SelectObject( pbmpOld );
    		ReleaseDC( pDC );
    
    		//Get Bits from that image
    		CImage atlImage;
    		atlImage.Load(OpenImageFilePath);
    
    		void* pPixel = NULL;
    		pPixel  = atlImage.GetBits();
    
    		int pitch = atlImage.GetPitch();
    		int bytes = abs(pitch) * atlImage.GetHeight();
    
    		const BYTE * src = NULL;
    		src = (BYTE*)atlImage.GetBits();
    
    		if(pitch < 0) src -= bytes;
    
    		BYTE * pBitmapData = NULL; 
    		pBitmapData = new BYTE[bytes];
    
    		memcpy(pBitmapData, src, bytes);
    		//memmove(pBitmapData, src, bytes); //Getting Error foe use memmove func also
    
    		width = atlImage.GetWidth();	
    		height = atlImage.GetHeight();
    		
    		for (int y = 0; y < width; y = y + 1)
    		{
    			iIndex = y + (width * height) - width; 
    			for (int x = 0; x < height ; x = x + 1)
    			{ 
    				OfflineImageData[iIndex] = (byte)(*(pBitmapData + iIndex));
    				OfflineData[y][x] = OfflineImageData[iIndex];
    				iIndex = iIndex - width; 
    			}
    		}
    	}//End OpenImageFileFlag
    	for (int y = 0; y < width; y = y + 1)//1024 ImageHeight
    	{
    		for (int x = 0; x < height-1 ; x = x + 1)//256 ImageWidth
    		{ 
    			SetPixelV(dc,25+y,50+x,RGB(OfflineData[y][x],OfflineData[y][x],OfflineData[y][x])); 
    		}
    	} 
    }
    Regards,

    SaraswathiSrinath

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,295

    Re: memcpy error in MFC

    memcpy usually only causes problems when either the source or destination pointers are invalid or you exceed the buffer size.

    Set a breakpoint on the memcpy line and verify each parameter has the values you expect.

  3. #3
    Join Date
    Nov 2011
    Location
    India
    Posts
    147

    Re: memcpy error in MFC

    Quote Originally Posted by Arjay View Post
    memcpy usually only causes problems when either the source or destination pointers are invalid or you exceed the buffer size.
    I Accept this. But I'm load 768&256 data & plot the same buffer size data only.

    Quote Originally Posted by Igor Vartanov View Post
    You do not provide a compilable project.
    Kindly find the attachment. Its a run time error.

    I'm getting 2 types of error.

    1. If i double click the OpenDlgFile path , then I'm getting Reading Vialation error. (Not all time. some time only getting this error)

    2. I'm getting 768 * 256 line data. But If i plot the data means, display contains 767 * 255 Only(View).(Line data missing)

    Kindly find the image files for your reference.

    Please clear me. Am i going right way?
    Attached Files Attached Files
    Regards,

    SaraswathiSrinath

  4. #4
    Join Date
    Nov 2011
    Location
    India
    Posts
    147

    Re: memcpy error in MFC

    Quote Originally Posted by Arjay View Post
    memcpy usually only causes problems when either the source or destination pointers are invalid or you exceed the buffer size.
    I find my mistake and cleared. Its a exceed buffer size problem.

    Code:
    CImage atlImage; CBitmap pBitmap1 ; BITMAP bm1;
    
    atlImage.Load(OpenImageFilePath);
    
    pBitmap1.Attach(atlImage.Detach()); 
    
    pBitmap1.GetObject( sizeof(BITMAP), &bm1 );
    
    width = bm1.bmWidth;    height = bm1.bmHeight;
    
    int pitch = width;
    
    int bytes = abs(pitch) * height; 
    
    BYTE * src = new BYTE[bytes];
    
    src = (BYTE*)bm1.bmBits;
    
    if(pitch < 0) src -= bytes;
    
    BYTE * pBitmapData = NULL;
    
    pBitmapData = new BYTE[bytes];
    
    memcpy(pBitmapData, src, bytes);

    Am i correct?
    Regards,

    SaraswathiSrinath

  5. #5
    Join Date
    Apr 1999
    Posts
    27,427

    Re: memcpy error in MFC

    Quote Originally Posted by saraswathisrinath View Post
    I find my mistake and cleared. Its a exceed buffer size problem.
    Code:
    BYTE * src = new BYTE[bytes];
    src = (BYTE*)bm1.bmBits;
    Do you see what's wrong with those two lines? Forget about bitmap data for a moment -- on a C++ level, do you see the issue with those two lines above?

    The new[] returns a pointer value. Then on the second line, you just throw it away and replace that value with another. How are you going to deallocate the memory if you throw away the original value?

    Your code is no different than just doing this:
    Code:
    src = (BYTE*)bm1.bmBits;
    but it's worse now that you've introduced a memory leak.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; June 23rd, 2013 at 11:31 PM.

  6. #6
    Join Date
    Nov 2011
    Location
    India
    Posts
    147

    Re: memcpy error in MFC

    I'm in beginner stage. If i'm doing any mistake, please take me to a right path at all time.

    I will replace my code. Thanks Mr.Paul
    Regards,

    SaraswathiSrinath

  7. #7
    Join Date
    Apr 1999
    Posts
    27,427

    Re: memcpy error in MFC

    Quote Originally Posted by saraswathisrinath View Post
    I'm in beginner stage. If i'm doing any mistake, please take me to a right path at all time.
    Code:
    int main()
    {
       int x = 0;
       int *p = new int [10];
       p = &x;
    }
    This is basically what your bit of code I pointed out is doing. You tell us -- do you see what's wrong with those three lines of code above? It gets worse if you then do this:
    Code:
    int main()
    {
       int x = 0;
       int *p = new int [10];
       p = &x;
       delete [] p;
    }
    What is wrong with the lines above? If you don't know, then you have a much larger issue than bitmap handling -- you need to know how to use the C++ language properly.

    Regards,

    Paul McKenzie

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: memcpy error in MFC

    Quote Originally Posted by saraswathisrinath View Post
    Code:
    CImage atlImage; CBitmap pBitmap1 ; BITMAP bm1;
    atlImage.Load(OpenImageFilePath);
    pBitmap1.Attach(atlImage.Detach()); 
    pBitmap1.GetObject( sizeof(BITMAP), &bm1 );
    width = bm1.bmWidth;    height = bm1.bmHeight;
    int pitch = width;
    int bytes = abs(pitch) * height; 
    BYTE * src = new BYTE[bytes];
    src = (BYTE*)bm1.bmBits;
    if(pitch < 0) src -= bytes;
    BYTE * pBitmapData = NULL;
    pBitmapData = new BYTE[bytes];
    memcpy(pBitmapData, src, bytes);
    Please, do not put more than one statement in the same line! Code written in such a manner (two or three statements per line) is very hard to read, very hard to debug and very hard to maintain!
    Besides, why do you insert an empty line after each line with code? It only makes sense if you separate some logical parts or blocks of your code, but there is no any in your code snippet!
    Victor Nijegorodov

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,977

    Re: memcpy error in MFC

    You do not provide a compilable project, and from your description it's not clear what kind of error you get. Is it compile time error? run time error? If run time, did you try to debug your code and inspect the parameters that were passed to memcpy?

    As for the WM_PAIN handler design, it seems you totally ignored all my comments provided in the other thread's post. So please be aware that you keep going wrong way.
    Best regards,
    Igor

  10. #10
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,389

    Re: memcpy error in MFC

    So what's the result of Arjay's request from post #2?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center