程序在输出BYTE*数据时内存响了好几声,并且free(pbBuffer)时出错:Debug Assertion Failed!,请大家帮忙!
sct02 2004-04-09 06:06:35 下面是我的程序的一段代码:
(功能是从源文件读出信息,
经过签名和加密处理形成新信息,然后将新信息写入目标文件)
BYTE *pbBuffer = NULL;
DWORD dwBlockLen = 2000; //每次从源文件读出2000byte
DWORD dwCount = 0;
BYTE *pbToBeSignedAndEncrypted = NULL; //要加密的数据
DWORD cbToBeSignedAndEncrypted = 0; //要加密的数据的大小
BYTE *pbSignedAndEncrypted = NULL; //加密形成的数据
DWORD cbSignedAndEncrypted = 0; //加密形成的数据的大小
// In a do loop, sign and encrypt the source file,
// and write to the source file.
do
{
// Read up to dwBlockLen bytes from the source file.
// Allocate memory.
if(pbBuffer = (BYTE *)malloc(dwBlockLen))
{
cout<<"Memory has been allocated for the buffer. \n";
}
else
{
MyHandleError("Out of memory. \n");
}
memset(pbBuffer,0,dwBlockLen); //先初始化
dwCount = fread(pbBuffer,1,dwBlockLen,hSource);
//hSource源文件
if(ferror(hSource))
{
MyHandleError("Error reading plaintext!\n");
}
//---------------------------------------------------
cout<<"See what have benn read1...\n\n";
ShowBytes(pbBuffer,dwCount);
cout<<"\nInput any char...\n";
cin>>c;
//---------------------------------------------------
//试着将读出的数据显示出来,从程序结果看是正确的
//下面将pbBuffer的内容传给pbToBeSignedAndEncrypted
cbToBeSignedAndEncrypted = dwCount;
if(pbToBeSignedAndEncrypted =
(BYTE *)malloc(cbToBeSignedAndEncrypted))
{
cout<<"Memory has been allocated for the buffer. \n";
}
memset(pbToBeSignedAndEncrypted,0,cbToBeSignedAndEncrypted);
//先初始化
pbToBeSignedAndEncrypted = pbBuffer;
//---------------------------------------------------
cout<<"\nSee what is in pbToBeSignedAndEncrypted...\n";
ShowBytes(pbToBeSignedAndEncrypted,cbToBeSignedAndEncrypted);
cout<<"\nInput any char...\n";
cin>>c;
//---------------------------------------------------
//试着将pbToBeSignedAndEncrypted的内容显示出来,
//从程序结果看,与前面那次显示结果一样,
//将pbBuffer的内容传给pbToBeSignedAndEncrypted没有问题
cbSignedAndEncrypted = 0;
pbSignedAndEncrypted = NULL;
//下面获取cbSignedAndEncrypted的大小
if(CryptSignAndEncryptMessage(
&SignPara,
&EncryptPara,
cRecipientCert,
rgpRecipientCert,
pbToBeSignedAndEncrypted,
cbToBeSignedAndEncrypted,
NULL,
&cbSignedAndEncrypted))
{
cout<<cbSignedAndEncrypted<<" bytes for the buffer .\n";
/////
cout<<"\nInput any char...\n";
cin>>c;
/////wei
}
else
{
MyHandleError("Getting the buffer length failed.");
}
//Allocated memory for the buffer
if(!(pbSignedAndEncrypted=(unsigned char *)
malloc(cbSignedAndEncrypted)))
MyHandleError("Memory allocation failed.");
//Call the function a second time to copy
//the signed and encrypted message into the buffer.
//真正进行签名、加密处理,将生成的数据存在pbSignedAndEncrypted中
if(CryptSignAndEncryptMessage(
&SignPara,
&EncryptPara,
cRecipientCert,
rgpRecipientCert,
pbToBeSignedAndEncrypted,
cbToBeSignedAndEncrypted,
pbSignedAndEncrypted,
&cbSignedAndEncrypted))
{
cout<<"The message is signed and encrypted.\n";
/////
cout<<"\nInput any char...\n";
cin>>c;
/////wei
}
else
{
MyHandleError("The message failed to sign and encrypt.");
}
//--------------------------------------------------------
cout<<"\nSee what is in pbSignedAndEncryptedBlob...\n";
ShowBytes(pbSignedAndEncryptedBlob,cbSignedAndEncryptedBlob);
cout<<"\nInput any char...\n";
cin>>c;
//--------------------------------------------------------
//执行这段代码时内存会响好几声,为什么?
//加密处理后的数据自然是乱码,但不至于显示乱码就会报警吧?
// Write data to the destination file
fwrite(pbSignedAndEncryptedBlob,1,
cbSignedAndEncryptedBlob,hDestination);
if(ferror(hDestination))
{
MyHandleError("Error writing ciphertext.");
}
//--------------------------------------------------------
if(pbBuffer)
free(pbBuffer);
if(pbToBeSignedAndEncrypted)
free(pbToBeSignedAndEncrypted);
if(pbSignedAndEncryptedBlob)
free(pbSignedAndEncryptedBlob);
//--------------------------------------------------------
//执行这段代码就会出错:Debug Assertion Failed!程序终止
//如果屏蔽掉这段代码,程序可以继续执行,这是为什么?
}
while(!feof(hSource));
cout<<"\n12345\n";
----------------------------------------------------------------
// Define the ShowBytes function.
void ShowBytes(BYTE *s, DWORD len)
{
DWORD TotalChars = 0;
DWORD ThisLine = 0;
while(TotalChars < len)
{
cout<<s[TotalChars];
TotalChars++;
}
cout<<"\n";
} // End of ShowBytes.
----------------------------------------------------------------
Debug Assertion Failed!
Program: F:*.exe(我的程序)
File: dbgheap.c
Line: 1011
Expression: _CrtIsValidHeapPointer(pUserData)
For information on how your program can cause
an assertion failure,see the Visual C++ documentation on asserts。
---------------------------------------------------------------
函数CryptSignAndEncryptMessage()和ShowBytes()
都是Microsoft SDK里面的代码,不会有错,
我估计问题出在哪几个BYTE*上,但不知具体出在哪里?
前两次调用ShowBytes()
cout<<"See what have benn read1...\n\n";
cout<<"\nSee what is in pbToBeSignedAndEncrypted...\n";
都可以将BYTE*的内容显示出来,
但为什么最后一次调用ShowBytes()
cout<<"\nSee what is in pbSignedAndEncryptedBlob...\n";时内存会响几声?
为什么那几个free()会导致Debug Assertion Failed!
恳请各位高手指点,谢谢!