导航
  • 主页
  • VC综合技术
  • 互联网技术
  • MFC AppLauncher
  • .NET 技术
  • 界面
  • 进程
  • 算法
  • 硬件/系统
  • 数据库
  • VC++技术资源

程序在输出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!
恳请各位高手指点,谢谢!






...全文
42 点赞 收藏 10
写回复
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ndy_w 2004-04-16

pbToBeSignedAndEncrypted = pbBuffer
改为
memcpy(pbSignedAndEncrypted, pbBuffer, dwCount);
不过,代码还是显得唐僧...
回复
sct02 2004-04-16
那是不是该改为:
pbBuffer = (BYTE *)malloc(dwBlockLen)
...
//pbToBeSignedAndEncrypted = (BYTE *)malloc(cbToBeSignedAndEncrypted)
//是要删掉这行吧?

pbToBeSignedAndEncrypted = pbBuffer
(这一句怎么写安全一些?
考虑到:
DWORD dwBlockLen = 2000; //每次从源文件读出2000byte
DWORD dwCount = 0;
pbBuffer = (BYTE *)malloc(dwBlockLen);
memset(pbBuffer,0,dwBlockLen);
dwCount = fread(pbBuffer,1,dwBlockLen,hSource);
//dwCount很可能小于dwBlockLen
cbToBeSignedAndEncrypted = dwCount;
//我要使pbToBeSignedAndEncrypted
//仅仅指向pbBuffer中的前dwCount个BYTE
//怎么写好一些?


...
pbSignedAndEncrypted=(unsigned char *)malloc(cbSignedAndEncrypted)))
...
free(pbBuffer);
free(pbSignedAndEncryptedBlob);


回复
jimlei76 2004-04-15
pbToBeSignedAndEncrypted = pbBuffer;//分配了的内存又对指针付值,该用memcpy.这是明显的内存泄漏。
free(pbBuffer);
free(pbToBeSignedAndEncrypted);
//两个指针是指向同一个内存块。多次free,当然有问题了。
回复
ndy_w 2004-04-15
pbBuffer = (BYTE *)malloc(dwBlockLen)
...
pbToBeSignedAndEncrypted = (BYTE *)malloc(cbToBeSignedAndEncrypted)
...
pbToBeSignedAndEncrypted = pbBuffer//????????干嘛?分配了又不用?丢空间了!
...
pbSignedAndEncrypted=(unsigned char *)malloc(cbSignedAndEncrypted)))
...
free(pbBuffer);
free(pbToBeSignedAndEncrypted); //=再次释放pbBuffer=>assert failed
free(pbSignedAndEncryptedBlob);
回复
ndy_w 2004-04-15
内存会响?!
响的原因是向cout输出了0x07字符,乱码嘛!正常.
回复
weicf01 2004-04-15

if(pbToBeSignedAndEncrypted)
free(pbToBeSignedAndEncrypted);
if(pbSignedAndEncryptedBlob)
free(pbSignedAndEncryptedBlob);屏掉,

if(pbBuffer)
free(pbBuffer);
放到后面一点看看(程序结束前)
回复
kuangjingbo 2004-04-09
我也看不出错在哪里!
回复
sct02 2004-04-09
我实在找不出错在哪里?大家帮看看吧!
回复
sct02 2004-04-09
你是说CryptSignAndEncryptMessage()吗?
这是Microsoft SDK里面的函数,传递适当的参数给它就可以了,
问题不在函数本身,BYTE* 参数上,但我就是搞不懂,大家帮看看吧!
回复
快乐鹦鹉 2004-04-09
检查一下,函数中有没有修改过这个指针的地址。
回复
发动态
发帖子
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……