Recently, PCM audio synthesis projects were doing a bug, which spent half a day. Of course, the modification is small, it is a very detailed question. However, I think about it, I have a similar experience, I feel a bit rule, write it out, maybe the Programmer has been encountered! :)
Below is a partially extracted code (color Visual Assist generated):
/ ** /
HRESULT CALMINPIN :: Convert (iMediasample * PIN, IMEDIASAMPLE * POUT)
{
HRESULT HR = S_OK;
/ * GET SUGGESTED SIZE OF DESTINATION BUFFER * /
Long len = pin-> getActualdatalength ();
DWORD suggested_dest_size = 0;
HR = acmstreamsize (M_HStream, Len, & SuggeSted_Dest_size, ACM_STREAMSIZEF_DESTINATION);
IF (hr! = mmsyserr_noerror)
Return HR;
Acmstreamheader ACMHDR;
Acmhdr.cbstruct = sizeof (acmhdr);
HR = Pin-> getPointer (& ACMHDR.PBSRC);
HR = pout-> getPointer (& ACMHDR.PBDST);
Acmhdr.cbsrclength = pin-> getActualDatalength ();
Acmhdr.cbdstlength = suggested_dest_size;
acmhdr.dwsrcuser = (dword) PIN;
acmhdr.dwdstuser = (dword) pout;
Acmhdr.dwuser = (dword) THIS;
acmhdr.fdwstatus =
0L;
//??? Always return Error Code 10
HR = ACMSTREAMPREPAREHEADER (M_HStream, & acmHDR, 0);
IF (hr! = mmsyserr_noerror)
Return HR;
/ * Send buffer to compressor / uncompressor (ACM) * /
HR = ACMSTREAMCONVERT (M_HStream, & AcmHDR, ACM_STREAMCONVERTF_BLOCKALIGN);
IF (hr! = mmsyserr_noerror)
{
Return HR;
}
HR = acmstreamunprepreprehead (m_hstream, & acmhdr, 0);
IF (hr! = mmsyserr_noerror)
Return HR;
Reference_time RTStart, Rtend;
Len = pin-> getActualDatalength ();
PIN-> GetTime (& RTSTART, & RTEND);
Rtend = RTSTART LEN;
Pout-> Settime (& RTStart, & Rtend);
Pout-> setActualDatalength; ACMHDR.CBDSTLENGTH;
Return S_OK;
}
//
Here, it involves the structure and function of DirectShow and ACM, but the idea is relatively clear. If you are careful, you can see these code, "Patchwork" coming out (raising the development speed!), From the source file included in the online source project and DirectShow. There is no problem with logically, of course, debugging has always had an error. The only clue is ERROR CODE 10, and Error Lookup is interpreted as "Environmental Error". What can I explain? Of course, the author doesn't know very well, so I spent a few hours, I learned some background knowledge, but there is nothing to find something worthless. Referring again to the code of other projects, but the C template syntax used by others, and it takes a lot of time to disassemble the key code, so they don't think about it.
Where is the idea? When some other attempts have passed, they don't work. Finally, I will debug someone else's code and can run through. One thing is sure, the process of the program is no problem, maybe some of the details are missing. After the two-segment code was found, some member variables of AcstreamHeader were not initialized. Is it really this problem? So the structural variable declats it.
MEMSET (& ACMHDR, 0, SIZEOF (ACMHDR));
Thus all members will initialize the 0. In debugging, the problem is solved! Later, I found the Acmapp Sample Project in MSDN, there is such a code:
...
PASH-> CBSTRUCT = SIZEOF (* PASH);
PASH-> fdwstatus =
0L;
PASH-> dwuser =
0L;
PASH-> PBSRC = PaACD-> PBSRC;
PASH-> CBSRCLENGTH = PaACD-> CBSRCReadsize;
PASH-> CBSRCLENGTHUSED =
0L;
PASH-> DWSRCUSER = PaACD-> CBSRCREADSIZE
PASH-> PBDST = PaACD-> PBDST;
PASH-> CBDSTLENGTH = PaACD-> CBDSTBUFSIZE;
PASH-> CBDSTLENGTHUSED =
0L;
PASH-> DWDSTUSER = PaACD-> CBDSTBUFSIZE;
...
The definition of this structure is as follows:
Typedef struct tacmstreamheader
{
DWORD CBSTRUCT; // Sizeof (acmstreamHeader)
DWord fdwstatus; // acmstreamheader_statusf_ *
DWORD dwuser; // user instance data for HDR
LPBYTE PBSRC;
DWORD CBSRCLENGTH;
DWORD CBSRCLENGTHUSED
DWORD DWSRCUSER; // User Instance Data For SRC
LPBYTE PBDST;
DWORD CBDSTLENGTH;
DWORD CBDSTLENGTHUSED; DWORD DWDSTUSER; // User Instance Data For DST
DWORD DWRESERVEDDRIVER [10]; // Driver Reserved Work Space
} Acmstreamheader, * pacmstreamheader, far * lpacmstreamheader;
Although 11 members do not have all, they are all assigned to DWRESERVEDDRIVER [10].
Summary: This type of error is caused by the non-assignment of certain structural members, and the variables that have not assigned in the debug mode are generally initialized to 0xcc. In the Release mode, it is usually 0x00 in the REE mode. Therefore, the compiler is assigned, but these values are wrong, so this must be processed during the encoding process. Either first initialize 0, either assign all related variables.
The writer has been written for 3 years, but it still makes this mistake, although it is more urgent. Therefore, it is necessary to develop good habits in us, and overcome the external pressure, write high quality code; rather than the wind fire rush, finally have to spend a lot of time to find the wrong, do not pay! This is the author to share with you!