DLL中内存分配再去释放的有关问题,提示:其原因可能是堆被损坏,这也说明demo.exe中或它所加载的任何 DLL 中有bug

DLL中内存分配再去释放的问题,提示:其原因可能是堆被损坏,这也说明demo.exe中或它所加载的任何 DLL 中有bug。
出错的 函数如下,为什么在函数里申请空间了再释放就出问题了呢?这个函数是属于我写的一个动态库中的。在外通过demo函数去进行功能测试。(刚才试验了下,在申请完毕后立刻释放却没有问题,这个是为什啊,难不成是因为整个过程中调用的函数改变了指针指向的空间?不至于啊,- -。真心求助)

int CASYoutubeURLParser::FindURL(void)
 {
char* cpBeginLocation = m_cDownloadURL;
char* cpMidLocation = NULL;
char* cpEndLocation = NULL;
char* beginFindExt = NULL;
char* endFindExt = NULL;
char* tempBuff = NULL;
char* beginKey = "http";
char* midKey = "type=video";
char* beginExtKey = "quality=";
char* endExtKey = "&";
int count = 0;/*用于统计影片的数量*/

//tempBuff =new char[256];
tempBuff = (char*) malloc (256*sizeof(char));
if( tempBuff != NULL)
{
memset(tempBuff, 0 , 256);
}
else
{
return -1;
}
do
{
cpBeginLocation = strstr(cpBeginLocation, beginKey);
if(cpBeginLocation == NULL){
break;
}

cpMidLocation = strstr( cpBeginLocation , midKey);
if(cpMidLocation == NULL){
break;
}

cpEndLocation = cpMidLocation + strlen(midKey);
int i=0;
do{
++cpEndLocation;
if( *cpEndLocation == ';' || *cpEndLocation == '&')
{
break;
}
++i;
}while(i<20);

/*找不到关键字就中断搜索*/
if(i == 20)
{
break;
}
/*
** 添加信息
** //获取URL信息
*/
strncpy( tempBuff, cpBeginLocation, cpEndLocation- cpBeginLocation);
tempBuff[cpEndLocation- cpBeginLocation] = '\0';
chr2wch( tempBuff, m_szActualDownloadURL[count] );
std::wcout<<m_szActualDownloadURL[count]<<std::endl;

/*获取类型*/
//strncpy( m_szActualDownloadFileExt[count], cpMidLocation+ strlen(midKey) + 1, cpEndLocation- strlen(midKey) -1);
//
///*保存影片的质量信息 如hd720等*/
//beginFindExt = strstr( cpBeginLocation, beginExtKey) + strlen(beginExtKey);
//endFindExt = strstr( beginFindExt, endExtKey );
//strncpy( m_szActualDownloadQuality[count], beginFindExt, endFindExt - beginFindExt);

++count;/*统计总共的影片数量*/
cpBeginLocation = cpEndLocation;
}while(cpBeginLocation != '\0');
/*保存数量*/
m_nActualDownloadVideoCount = count;
if( tempBuff != NULL )
{
//delete[] tempBuff;
free(tempBuff);
}
return 0;
 }

函数中中调用的函数如下
void CASYoutubeURLParser::chr2wch( char* buffer, ASChar* wBuf)
{
size_t len = strlen(buffer);
size_t wlen = MultiByteToWideChar(CP_ACP, 0, (const char*)buffer, int(len), NULL, 0);
MultiByteToWideChar(CP_ACP, 0, (const char*)buffer, int(len), wBuf, int(wlen));
}
两处的反汇编结果如下:
  tempBuff = (char*) malloc (256*sizeof(char));
10004F65 mov esi,esp 
10004F67 push 100h 
10004F6C call dword ptr [__imp__malloc (10007150h)] 
10004F72 add esp,4 
10004F75 cmp esi,esp 
10004F77 call _RTC_CheckEsp (10005162h) 
10004F7C mov dword ptr [tempBuff],eax 

//delete[] tempBuff;
free(tempBuff);
100050C3 mov esi,esp 
100050C5 mov eax,dword ptr [tempBuff] 
100050C8 push eax  
100050C9 call dword ptr [__imp__free (10007160h)] 
100050CF add esp,4 
100050D2 cmp esi,esp 
100050D4 call _RTC_CheckEsp (10005162h)

------解决方案--------------------
这种提示应该是指针被带出本工程项目之外了吧,楼主检查一下吧
------解决方案--------------------
tempBuff[cpEndLocation- cpBeginLocation] = '\0';
chr2wch( tempBuff, m_szActualDownloadURL[count] );

看看是否越界了
------解决方案--------------------