|
-
February 4th, 2007, 11:31 PM
#1
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;
}
-
February 5th, 2007, 12:52 AM
#2
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
-
February 5th, 2007, 02:06 AM
#3
-
February 5th, 2007, 07:17 AM
#4
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]
-
February 5th, 2007, 07:20 AM
#5
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.
-
February 5th, 2007, 07:33 AM
#6
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?
-
February 5th, 2007, 11:35 AM
#7
Re: memcpy error
 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
-
February 5th, 2007, 04:56 PM
#8
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|