CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: memcpy error

  1. #1
    Join Date
    Dec 2006
    Posts
    17

    Angry memcpy error

    I have an application that will capture sound from a mic in real-time. The application runs but when I have finished recording the sound I am receiving an error with the memcpy command. The error outputs a messagebox that says:

    "Unhandled exception at 0x004739ca in capturesound.exe: 0xC0000005: Access violation writing location 0x00000000."

    This error is on the "memcpy.asm" which is on the line:

    "rep movsd ;N - move all of our dwords"

    Here is the part where I used memcpy:

    //-----------------------------------------------------------------------------
    // Name: RecordCapturedData()
    // Desc: Copies data from the capture buffer to the output buffer
    //-----------------------------------------------------------------------------
    HRESULT RecordCapturedData()
    {
    HRESULT hr;
    VOID* pbCaptureData = NULL;
    DWORD dwCaptureLength;
    VOID* pbCaptureData2 = NULL;
    DWORD dwCaptureLength2;
    DWORD dwDataWrote;
    DWORD dwReadPos;
    DWORD dwCapturePos;
    LONG lLockSize;
    void* buf;

    if( NULL == g_pDSBCapture )
    return S_FALSE;
    //if( NULL == g_pWaveFile )
    // return S_FALSE;

    if( FAILED( hr = g_pDSBCapture->GetCurrentPosition( &dwCapturePos, &dwReadPos ) ) )
    return DXTRACE_ERR_MSGBOX( TEXT("GetCurrentPosition"), hr );

    lLockSize = dwReadPos - g_dwNextCaptureOffset;
    if( lLockSize < 0 )
    lLockSize += g_dwCaptureBufferSize;

    // Block align lock size so that we are always write on a boundary
    lLockSize -= (lLockSize % g_dwNotifySize);

    if( lLockSize == 0 )
    return S_FALSE;

    // Lock the capture buffer down
    if( FAILED( hr = g_pDSBCapture->Lock( g_dwNextCaptureOffset, lLockSize,
    &pbCaptureData, &dwCaptureLength,
    &pbCaptureData2, &dwCaptureLength2, 0L ) ) )
    return DXTRACE_ERR_MSGBOX( TEXT("Lock"), hr );


    if(pbCaptureData) memcpy(buf,pbCaptureData,dwCaptureLength);
    if(pbCaptureData2) memcpy((char*)
    buf+dwCaptureLength,pbCaptureData2,dwCaptureLength2);


    // Move the capture offset along
    g_dwNextCaptureOffset += dwCaptureLength;
    g_dwNextCaptureOffset %= g_dwCaptureBufferSize; // Circular buffer


    // Move the capture offset along
    g_dwNextCaptureOffset += dwCaptureLength2;
    g_dwNextCaptureOffset %= g_dwCaptureBufferSize; // Circular buffer
    }

    // Unlock the capture buffer
    g_pDSBCapture->Unlock( pbCaptureData, dwCaptureLength,
    pbCaptureData2, dwCaptureLength2 );


    return S_OK;
    }

  2. #2
    Join Date
    Sep 2006
    Location
    Sunshine State
    Posts
    517

    Re: memcpy error

    Hi.

    You're attempting to copy data to a pointer that points to NULL.
    E.g. you haven't allocated memory for the ptr.


    -Andy

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: memcpy error

    Take a look at this FAQ.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Dec 2006
    Posts
    17

    Unhappy Re: memcpy error

    I have opened the Call Stack and the green arrow points to this message: ">capturesound.exe!memcpy(unsigned char * dst=0x00000000, unsigned char * src=0x013b0028, unsigned long count=65000) Line 188 Asm"

    I do not know how to debug this kind of error. Is this because of the allocation of the memory for the destination "buf"? If yes, how can I make a memory space for that. Can anyone help...

    here is my code:

    [source]
    //-----------------------------------------------------------------------------
    // Name: RecordCapturedData()
    // Desc: Copies data from the capture buffer to the output buffer
    //-----------------------------------------------------------------------------
    HRESULT RecordCapturedData()
    {
    HRESULT hr;
    VOID* pbCaptureData ; //= NULL;
    DWORD dwCaptureLength;
    VOID* pbCaptureData2 ;//= NULL;
    DWORD dwCaptureLength2;
    DWORD dwDataWrote;
    DWORD dwReadPos;
    DWORD dwCapturePos;
    LONG lLockSize;
    void* buf;

    if( NULL == g_pDSBCapture )
    return S_FALSE;

    if( FAILED( hr = g_pDSBCapture->GetCurrentPosition( &dwCapturePos, &dwReadPos ) ) )
    return DXTRACE_ERR_MSGBOX( TEXT("GetCurrentPosition"), hr );

    lLockSize = dwReadPos - g_dwNextCaptureOffset;
    if( lLockSize < 0 )
    lLockSize += g_dwCaptureBufferSize;

    // Block align lock size so that we are always write on a boundary
    lLockSize -= (lLockSize % g_dwNotifySize);

    if( lLockSize == 0 )
    return S_FALSE;

    // Lock the capture buffer down
    if( FAILED( hr = g_pDSBCapture->Lock( g_dwNextCaptureOffset, lLockSize,
    &pbCaptureData, &dwCaptureLength,
    &pbCaptureData2, &dwCaptureLength2, 0L ) ) )
    return DXTRACE_ERR_MSGBOX( TEXT("Lock"), hr );


    if(pbCaptureData) memcpy(buf,pbCaptureData,dwCaptureLength);
    if(pbCaptureData2) memcpy((char*)buf+dwCaptureLength,pbCaptureData2,dwCaptureLength2);

    g_writefile = WriteFile(g_hCreateFile,
    pbCaptureData,
    dwCaptureLength,
    &dwDataWrote,
    NULL);

    // Move the capture offset along
    g_dwNextCaptureOffset += dwCaptureLength;
    g_dwNextCaptureOffset %= g_dwCaptureBufferSize; // Circular buffer


    g_writefile = WriteFile(g_hCreateFile,
    pbCaptureData2,
    dwCaptureLength2,
    &dwDataWrote,
    NULL);

    // Move the capture offset along
    g_dwNextCaptureOffset += dwCaptureLength2;
    g_dwNextCaptureOffset %= g_dwCaptureBufferSize; // Circular buffer


    // Unlock the capture buffer
    g_pDSBCapture->Unlock( pbCaptureData, dwCaptureLength,
    pbCaptureData2, dwCaptureLength2 );

    return S_OK;

    }

    [/source]

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: memcpy error

    With this:
    Code:
    VOID* pbCaptureData ; //= NULL;
    DWORD dwCaptureLength;
    VOID* pbCaptureData2 ;//= NULL;
    you made the worst programming error: left your variables uninitialized.

    It seems you don't understand the problem: you have to allocate memory for the buffers you are using, and failure to do that will end in a run-time failure of your application.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Dec 2006
    Posts
    17

    Re: memcpy error

    That part is originally written in this way...

    VOID* pbCaptureData ; = NULL;
    DWORD dwCaptureLength;
    VOID* pbCaptureData2 ;= NULL;

    I have confirmed that my buffers are working properly, not until that I want to copy the data in the capture buffer to a memory space for analysis lets say fourier transform of the data. I am passing the data to "buf" which I have initialized to "void* buf;" is memory allocated in this manner?

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

    Re: memcpy error

    Quote Originally Posted by sap00
    I have confirmed that my buffers are working properly,
    So everything is OK then, right? Or is there still a problem?
    I am passing the data to "buf" which I have initialized to "void* buf;" is memory allocated in this manner?
    All you've declared is a pointer -- nothing is allocated.

    The pointer must point to memory that you've allocated dynamically, or a static buffer. Either way, both must be large enough to hold the data you're copying.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Aug 2005
    Posts
    59

    Re: memcpy error

    When you declare a pointer you must allocate memory to it..

    eg...

    char * pData;
    pData = new char[20];

    This is rather simple but it emphasizes the point that we have now allocated effectively 20 bytes of memory which we can then memcpy into.

    Alternatively if you have a data structure to accept TGA files

    Code:
    struct TgaHeader {
    		char    IDLen;         //length of ID field
    		char    MapType;       //0=none, 1=present
    		char    ImgType;       //0=none, 1=c-map, 2=true, 3=b&w, 9=rle c-map, 10=rle true, 11=rle b&w
    		short   FirstColor;    //where to start in colormap
    		short   MapLen;        //number of colors to set
    		char    EntrySize;     //number of bits per color entry
    		short   Xorigin;
    		short   Width;
    		short   Height;
    		char    Depth;
    		char    Descriptor;
    	};
    When we want to load a new TGA we will declare a pointer to the texture
    Code:
    TgaHeader * pTGA;
    But we then must allocate memory to it
    Code:
    pTGA = new TgaHeader;
    In your example above you declare void * buf but you never tell it how much memory you want to reserve for buf

    Something like this might work...although my c++ skills are rather limited

    Code:
    unsigned uDecompressBuffer[ MAX_CAPTURE_SIZE];
    		pbufPointer = (unsigned *)(((char *)&uDecompressBuffer) );
    		buf = (void *)pbufPointer;
    Where you can define a max capture size in the h file. Alternatively if you know what the capture size is replace MAX_CAPTURE_SIZE with a variable that is at least equal to the size of the capture stream

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