//------------------------------------------------------------------------------
//接 收 短 消 息                                                               /
//------------------------------------------------------------------------------
function TForm1.receiveMessage():integer;
var
  sm_ID,UDLen:DWORD;
  DestTON,DestNPI,OrgTON,OrgNPI:byte;
  PRI,RP,UDHI,PID,DCS,StatusReport:byte;
  OrgAddr,DestAddr,UserData,TimeStamp:PChar;
  sMsgID,sSubmitDate,sDonedate,sStatus:PChar;
  tmpUid,tmpUid2,orderCode:string;
  tmpOrgAddr,tmpDestAddr:string;
begin//如果登陆成功,数据库连接成功,端口初始化成功
if((online=true)and (dbStatus=true) and (portStatus=true)) then
begin
  if (HasDeliverMessage(100)=0) then
  begin    GetMem(OrgAddr,32);
    GetMem(DestAddr,20);
    GetMem(UserData,140);
    GetMem(sMsgID,23);
    GetMem(TimeStamp,20);
    GetMem(sSubmitDate,11);
    GetMem(sDoneDate,11);
    GetMem(sStatus,80);
   if GetDeliverSMExExEx(sm_ID,DestTON,DestNPI,DestAddr,OrgTON,OrgNPI,OrgAddr,PRI,RP,UDHI,PID,DCS,
     TimeStamp,UDLen,UserData,StatusReport,sMsgID,sSubmitDate,sDonedate,sStatus) = 0 then
    begin
      if StatusReport = 0 then
       begin
       orderCode := copy(DestAddr,8,2);              if (orderCode='11') then //手机回复平台
                    begin
                         tmpOrgAddr := copy(OrgAddr,3,11);                      //把 src号码 (8613798391986)的前面 86 去掉,
                         tmpDestAddr:= copy(DestAddr,10,11);                    //把 desc号码 (04441361013714776831) 的 044136 10去掉
                         SQLDataSet3.Close;                                     //从sms_phone 根据号码找到 uid ,填入sms_sendSingle
                         SQLDataSet3.CommandText:='select sm_user_uid from sms_phone where((ChinaTelecom="'+tmpDestAddr+'")  or (ChinaMobil="'+tmpDestAddr+'") or (ChinaUnicom="'+tmpDestAddr+'"))';
                         SQLDataSet3.Open;
                         tmpUid     := string(SQLDataSet3.FieldValues['sm_user_uid']);
                         if not SQLDataSet3.Eof then
                              begin
                              SQLDataSet4.Close;
                              SQLDataSet4.CommandText:='Insert into sms_sendSingle(srctermid,desttermid,msgcontent,return,uid,orderCode,reserved)';
                              SQLDataSet4.CommandText:=SQLDataSet4.CommandText +'Values("'+tmpOrgAddr+'","'+tmpDestAddr+'","'+UserData+'","2","'+tmpUid+'","'+orderCode+'","接收")';
                              SQLDataSet4.ExecSQL;
                              //SQLDataSet3.Next;
                              end;
                    end
               else
                     begin
                     end;
                     memo1.Clear;
                     memo1.lines.add(TimeToStr(Now)+ '收到短消息:'+' 来自:'+OrgAddr+' 发送到: '+DestAddr);
                     memo1.lines.add('信息内容为:"'+UserData+'","'+tmpUid+'"');        end
        else
                  begin
                     memo1.lines.add(TimeToStr(Now)+'收到状态报告:sMsgID='+sMsgID+' 发送时间='+sSubmitDate+' 终结时间='+sDonedate+' 最终状态='+sStatus);
                  end;
        end;
  
      if OrgAddr <>nil then dispose(OrgAddr);
      if DestAddr <>nil then dispose(DestAddr);
      if UserData <>nil then dispose(UserData);
      if sMsgID <> nil then dispose(sMsgID);
      if TimeStamp <>nil then dispose(TimeStamp);
      if sSubmitDate <> nil then dispose(sSubmitDate);
      if sDoneDate <> nil then dispose(sDoneDate);
      if sStatus <> nil then dispose(sStatus); 
  end; //HasDeliverMessage end;  //如果登陆成功,数据库连接成功,端口初始化成功 end;

解决方案 »

  1.   

    if OrgAddr <>nil then 
    begin
      dispose(OrgAddr);
      OrgAddr = nil;
    end;其他都一样。。
      

  2.   

    定长的 直接在栈上声明对象 即可。。使用到指针的话 就直接取地址 @..
    .....还有你如果是pchar型的话 最好多申请一个字节的空间 好放 \0终结符
      

  3.   

    getMem 要與 freeMem 對應
      

  4.   

    谢谢 大家
    beyondtkl(大龙驹<暗黑系魔法师&&赏金猎人>) 说对了,pchar型的话 最好多申请一个字节的空间 好放 \0终结符 w我多申请了一个,可以了,谢谢