在class的public中定义变量(CString strsend_all[15]; 
CString strsend_cmgl),在OnInitDialog()中初始化此变量,strsend_all[0]="0891683108100005F0040D91683196533746F600F620010312130002710464026F050005000401241736999999241737000000241737123E452417381234E62417381E2345241738432E77241739345E99241739234E782417401E23542417403234E82417412134E724174144E444241742111111241742222222241743333333241743444444FFFFFFFF1A9803";
strsend_all[1]=strsend_all[0];
strsend_all[2]=strsend_all[0];
strsend_all[3]=strsend_all[0];
strsend_all[4]=strsend_all[0];
strsend_all[5]=strsend_all[0];
strsend_all[6]=strsend_all[0];
strsend_all[7]=strsend_all[0];
strsend_all[8]=strsend_all[0];
strsend_all[9]=strsend_all[0];
strsend_all[10]=strsend_all[0];
strsend_all[11]=strsend_all[0];
strsend_all[12]=strsend_all[0];
strsend_all[13]=strsend_all[0];
strsend_all[14]=strsend_all[0];反复调用下函数:
void CAT_AUTOCOMDlg::record_dir()   //AT+CMGL=4\r\n
{
CString enter=0x0D;              
CString newline=0x0A;
CString strsend_OK=enter+newline+"OK"+enter+newline;
CString blank=0x20;
CString temp1=",1,,133";
int i; for(i=0;i<15;i++)
{
if (strsend_all[i]!="")
{
char strnum[10];
itoa(i+1,strnum,10);  
strsend_cmgl+=strsend_all[i];//*****
}
}
m_Port[1].WriteToPort(strsend_cmgl);//写到串口
strsend_cmgl.Empty();
     strsend_cmgl.FreeExtra();
}
上面的函数能调用5到6次,完后就是:内存0XXXXXXXX不能为读或不能为写之内的错误!有时弹出“内存不足”的对话框。
如果将*****处的改为:strsend_cmgl+="+CMGL:"+blank+strnum+temp1
                       +enter+newline+strsend_all[i]+enter+newline;
后该函数只能被调用一次,第二次调用时运行到上述语句出出现:内存0XXXXXXXX不能为读或不能为写之内的错误!或者“内存不足”的对话框!
当前错误位置是:D:\...\VC98\CRT\SBHEAP.C//  change size of free entry (front and back)
    if (sizeNewFree != 0)
    {
        pEntry->sizeFront = sizeNewFree;
        ((PENTRYEND)((char *)pEntry + sizeNewFree -
                    sizeof(ENTRYEND)))->sizeBack = sizeNewFree;//这里
*********************************************************************
void CSerialPort::WriteToPort(LPCTSTR string)
{
assert(m_hComm != 0); memset(m_szWriteBuffer, 0, sizeof(m_szWriteBuffer));
strcpy(m_szWriteBuffer, string);
m_nWriteSize=strlen(string); // set event for write
SetEvent(m_hWriteEvent);
}
荡的一个串口类(CSerialPort)中的WriteToPort函数
********************************************************************
麻烦大家指点指点,发表看法或指出错误!我是新手,有什么不规范的也可以指出!
有什么需要进一步了解的尽管“开口”,我现在烦死了!
谢谢!

解决方案 »

  1.   

    strsend_cmgl+=strsend_all[i];
    告诉你,CString 不能超直65535也就是64k
    你这么 反复的调用*15,也没有清空,数量也是很惊人的
    会不会超过了64K
      

  2.   


    如果将
    strsend_all[0]="0891683108100005F0040D91683196533746F600F620010312130002710464026F050005000401241736999999241737000000241737123E452417381234E62417381E2345241738432E77241739345E99241739234E782417401E23542417403234E82417412134E724174144E444241742111111241742222222241743333333241743444444FFFFFFFF1A9803";
    改为
    strsend_all[0]="12342342424';record_dir()函数可以调用n次(n>=20)没问题!!!!!!
      

  3.   

    在以前的 MFC 版本中,在使用 CString 对象后进行清理是很重要的。使用 MFC 3.0 或更高版本,就不再需要进行显式清理了。在 MFC 现在使用的 C++ 异常处理机制下,您不必担心异常之后的清理。有关 C++ 如何在捕获异常之后“展开”堆栈的说明,请参阅 try、catch 和 throw 语句。即便使用 MFC 的 TRY/CATCH 宏(而不是 C++ 的关键字 try 和 catch),MFC 也会使用底层的 C++ 异常机制,这样您仍然不需要进行显式清理。
      

  4.   

    您可以写一个程序测试一个单独的CString对象能够包含字符的长度
      

  5.   

    CString不是指向字符串的指针吗!上面的CString对象最大也只到4000多字节,应该没有问题吧!我的条子不大,也有128M,不会吧!!!!
      

  6.   

    这里不规范:字符串不能分行写。
    strsend_all[0]="0891683108100005F0040D91683196533746F600F6200103121300027" \ 
    "10464026F050005000401241736999999241737000000241737123E4524" \
    "17381234E62417381E2345241738432E77241739345E99241739234E782" \
    "417401E23542417403234E82417412134E724174144E444241742111111" \
    "241742222222241743333333241743444444FFFFFFFF1A9803";
      

  7.   

    我把你的代码拷贝下来,试了一下,没有问题(去掉了strsend_cmgl.FreeExtra();),看来问题不在这里,是不是写串口的问题。有没有可能是串口的缓冲区的问题。
      

  8.   

    把最后两句去掉看看。
    strsend_cmgl.Empty();
    strsend_cmgl.FreeExtra();
      

  9.   

    我去掉这两条,
    strsend_cmgl.Empty();
    strsend_cmgl.FreeExtra();
    用strsend_cmgl+="+CMGL:"+blank+strnum+temp1
                           +enter+newline+strsend_all[i]+enter+newline;
    能调用成功三次,然后依然!writebuffersize=512 根本动不的,一改动,一次都成功不了!
      

  10.   

    也有128M?你是不是说你的内存有128M,
    其实在Window的虚拟内存机制下,可以用硬盘来模拟物理内存的
    每人程序可以用4G的空间,不可能存在内存不足的情况,一般来说
    刚才看了一下MFC
    A CString object can store up to INT_MAX (2,147,483,647) characters. The TCHAR data type is used to get or set individual characters inside a CString object. Unlike character arrays, the CString class has a built-in memory allocation capability. This allows CString objects to automatically grow as needed (that is, you don’t have to worry about growing a CString object to fit longer strings). 
    可知cstring的字符长度是很大的,达到A CString object can store up to INT_MAX (2,147,483,647)
    所以你还是用调试,一步一步跟踪,看看到底出在哪里了
    经过多次跟踪,宿小范围,找到问题所在点
      

  11.   

    倒是觉得你这里有问题
    strcpy(m_szWriteBuffer, string);
    m_nWriteSize=strlen(string);szWriteBuffer是从哪里来的,它有多大
    m_nWriteSize是怎么定义的
    strcpy是把后面的加到前面去Res
    The strcpy function copies strSource, including the terminating null character, to the location specified by strDestination. No overflow checking is performed when strings are copied or appended. The behavior of strcpy is undefined if the source and destination strings overlap.Example
    /* STRCPY.C: This program uses strcpy
     * and strcat to build a phrase.
     */#include <string.h>
    #include <stdio.h>void main( void )
    {
       char string[80];
       strcpy( string, "Hello world from " );
       strcat( string, "strcpy " );
       strcat( string, "and " );
       strcat( string, "strcat!" );
       printf( "String = %s\n", string );
    }Output
    String = Hello world from strcpy and strcat!
      

  12.   

    去掉
    m_Port[1].WriteToPort(strsend_cmgl);//写到串口
    后,
    一切正常。会不会是它的问题?