LPTSTR lpChar  = strJpnRule.GetBuffer(strJpnRule.GetLength());
while(*lpChar){
char *pNext = lpChar + strcspn(lpChar, "|");
if((pNext > lpChar)&&(*pNext == '|')){
char cKeep = *pNext;
*pNext = 0;
RuleArray.Add(lpChar);
*pNext = cKeep;
lpChar = pNext + 1;
}else if (*lpChar){
lpChar += strlen(lpChar);
}    }在這裡RuleArray.Add(lpChar)总是出错。有时候在STRING的Left()、Mid()、+=等函数那里出错。
都是系统函数申请空间错误、我检查了下 New 和 delete 、也没有发现什么错误。望高手指点一二。
出错原因如下:
_heap_alloc_base(unsigned int 0x00000050) line 200
_heap_alloc_dbg(unsigned int 0x0000001f, int 0x00000001, const char * 0x006ee788 THIS_FILE, int 0x00000076) line 378 + 9 bytes
_nh_malloc_dbg(unsigned int 0x0000001f, int 0x00000000, int 0x00000001, const char * 0x006ee788 THIS_FILE, int 0x00000076) line 248 + 21 bytes
_malloc_dbg(unsigned int 0x0000001f, int 0x00000001, const char * 0x006ee788 THIS_FILE, int 0x00000076) line 165 + 27 bytes
operator new(unsigned int 0x0000001f, int 0x00000001, const char * 0x006ee788 THIS_FILE, int 0x00000076) line 373 + 21 bytes
operator new(unsigned int 0x0000001f, const char * 0x006ee788 THIS_FILE, int 0x00000076) line 65 + 19 bytes
CString::AllocBuffer(int 0x00000012) line 118 + 19 bytes
CString::AllocBeforeWrite(int 0x00000012) line 202
CString::AssignCopy(int 0x00000012, const char * 0x01e53204) line 315
CString::operator=(const char * 0x01e53204) line 346
CStringArray::SetAtGrow(int 0x00000000, const char * 0x01e53204) line 233
CStringArray::Add(const char * 0x01e53204) line 242

解决方案 »

  1.   

    LPTSTR lpChar  = strJpnRule.GetBuffer((strJpnRule.GetLength()+1)*sizeof(TCHAR));
      

  2.   

    快来帮忙啊! 不是GetBufer函数的问题!
      

  3.   

    GetBuffer有问题,造成heap被破坏,所以有上面的错误。
      

  4.   

    我已经按你的改过了、还是不行。我用其他程序试这个类没有问题。只是现在用的这个程序有问题、在这个程序其他的地方用我写的类时、有时会在 Left()、Mid()、+=等地方出错。老是找不到原因、郁闷啊。最后出错都dhiHeap出错。究竟错在哪里呢?
      

  5.   

    RuleArray类型是什么?以下两种都没问题
    CArray<CString,CString> RuleArray;
    CArray<LPTSTR,LPTSTR> RuleArray;//应该不是这种
      

  6.   

    如果我没猜错的话,问题就在这里拉  
    LPTSTR lpChar  = strJpnRule.GetBuffer(strJpnRule.GetLength());
           
             strJpnRule.ReleaseBuffer();//你可以试试 while(*lpChar){
    char *pNext = lpChar + strcspn(lpChar, "|");
    if((pNext > lpChar)&&(*pNext == '|')){
    char cKeep = *pNext;
    *pNext = 0;
    RuleArray.Add(lpChar);
    *pNext = cKeep;
    lpChar = pNext + 1;
    }else if (*lpChar){
    lpChar += strlen(lpChar);
    }    }
      

  7.   

    strJpnRule.ReleaseBuffer();这句我写过了、写在循环的最后
    RuleArray的类型是 CStringArray
    请问如何检查一下堆的分配?
      

  8.   

    没有人回答啊!再次请教能否根据出错的内存地址判断出是那个变量出的错呢?
    比如说根据Heap corruption detected at 01E98BA8  找出这个01E98BA8是什么内容?
      

  9.   

    根本的原因是你不理解CString的内存处理机制。你把CString分配的内存地址拿来用,要知道,当CString释放或CString自动增加内存空间时,其原来分配的内存地址就无效了,但你的RuleArray还在用,因而就出错了。正确的做法是复制出值,或显式重建一个CString对象并在RuleArray清除时进行显示释放,
      

  10.   

    再次请教 我怎么把CString分配的内存地址拿出来用拉?举个例子。
    我就是把RuleArray里CString 数据拿出来 进行处理。
    比如说 StrTemp = StrTemp.Mid(9)等操作、也不行吗? 
    最后RuleArray需要清除吗? 这个是局部变量不需要清除吧!如果清除
    RuleArray。RemoveAll()就可以吧、里面元素没有New出来的。
      

  11.   

    LPTSTR lpChar  = strJpnRule.GetBuffer(strJpnRule.GetLength());
    ===================改成
    LPTSTR lpChar = new char[strJpnRule.GetLength()+1];
    _tcscpy(lpsz, strJpnRule);
    最简单的LPTSTR lpChar = (LPTSTR)(LPCTSTR)strJpnRule;
      

  12.   

    最简单的LPTSTR lpChar = (LPTSTR)(LPCTSTR)strJpnRule;
    这个在这里也不能用。
    LPTSTR lpChar = new TCHAR[strJpnRule.GetLength()+1];
    _tcscpy(lpsz, strJpnRule);