我的一个Socket服务端程序,线程阻塞模式的,最近测试100个客户端并发的时候总是报内存地址访问冲突: Access Violation...错误,我用FastMM查内存有无泄露,以下是日志:--------------------------------2011/5/30 16:00:10--------------------------------
FastMM has detected an attempt to call a virtual method on a freed object. An access violation will now be raised in order to abort the current operation.Freed object class: TClientBlockThrVirtual method: AfterConstructionVirtual method address: 4278C8The allocation number was: 408412The object was allocated by thread 0x1440, and the stack trace (return addresses) at the time was:
402DC0 [jpeg][jpeg][@GetMem]
403F77 [System][System][TObject.NewInstance]
404352 [System][System][@ClassCreate]
48F1C6 [ScktComp][ScktComp][TServerClientThread.Create]
4C2DD5 [uMain.pas][uMain][TfrmMain.SerSktThr][254]
48F078 [ScktComp][ScktComp][TServerWinSocket.GetServerThread]
48ECA4 [ScktComp][ScktComp][TServerWinSocket.Accept]
48F1AA [ScktComp][ScktComp][TServerAcceptThread.Execute]
42771F [Classes][Classes][ThreadProc]
404DE6 [System][System][ThreadWrapper]
7C80B729 [Unknown function at GetModuleFileNameA]The object was subsequently freed by thread 0x860, and the stack trace (return addresses) at the time was:
402DEB [jpeg][jpeg][@FreeMem]
403F95 [System][System][TObject.FreeInstance]
40439D [System][System][@ClassDestroy]
48F25E [ScktComp][ScktComp][TServerClientThread.Destroy]
403FDB [System][System][TObject.Free]
427776 [Classes][Classes][ThreadProc]
404DE6 [System][System][ThreadWrapper]
7C80B729 [Unknown function at GetModuleFileNameA]The current thread ID is 0x1440, and the stack trace (return addresses) leading to this error is:
4043AA [System][System][@AfterConstruction]
48F216 [ScktComp][ScktComp][TServerClientThread.Create]
4C2DD5 [uMain.pas][uMain][TfrmMain.SerSktThr][254]
48F078 [ScktComp][ScktComp][TServerWinSocket.GetServerThread]
48ECA4 [ScktComp][ScktComp][TServerWinSocket.Accept]
48F1AA [ScktComp][ScktComp][TServerAcceptThread.Execute]
42771F [Classes][Classes][ThreadProc]
404DE6 [System][System][ThreadWrapper]
7C80B729 [Unknown function at GetModuleFileNameA]Current memory dump of 256 bytes starting at pointer address 7FCE3BE0:
8C BC 4C 00 80 80 80 80 80 80 80 80 80 80 00 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 9C 1B 5F 82 80 80 80 80
80 80 80 80 80 80 80 80 00 00 00 00 70 1A CE 7F 00 00 00 00 00 00 00 00 D8 A6 40 00 00 00 00 00
E3 57 02 00 C0 2D 40 00 15 4F 40 00 40 4F 40 00 A8 F1 40 00 DC D4 46 00 9B 8C 46 00 A1 1C 4A 00
DC D4 46 00 9B 8C 46 00 9B 8C 46 00 CA DE 46 00 B4 03 00 00 EB 2D 40 00 95 3F 40 00 9D 43 40 00
C3 78 42 00 DB 3F 40 00 76 77 42 00 E6 4D 40 00 29 B7 80 7C 00 00 00 00 00 00 00 00 00 00 00 00
BC 17 00 00 62 00 00 00 EC 36 4A 00 BA 7C 96 01 01 00 00 00 58 00 00 00 33 30 2F 30 35 2F 32 30
? ? L  .  €  €  €  €  €  €  €  €  €  €  .  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €
€  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €
€  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  ? .  _  ? €  €  €  €
€  €  €  €  €  €  €  €  .  .  .  .  p  .  ?   .  .  .  .  .  .  .  .  ? ? @  .  .  .  .  .
? W  .  .  ? -  @  .  .  O  @  .  @  O  @  .  ? ? @  .  ? ? F  .  ? ? F  .  ? .  J  .
? ? F  .  ? ? F  .  ? ? F  .  ? ? F  .  ? .  .  .  ? -  @  .  ? ?  @  .  ? C  @  .
? x  B  .  ? ?  @  .  v  w  B  .  ? M  @  .  )  ? €  |  .  .  .  .  .  .  .  .  .  .  .  .
? .  .  .  b  .  .  .  ? 6  J  .  ? |  ? .  .  .  .  .  X  .  .  .  3  0  /  0  5  /  2  0--------------------------------2011/5/30 17:58:22--------------------------------
FastMM has detected an attempt to call a virtual method on a freed object. An access violation will now be raised in order to abort the current operation.Virtual method: AfterConstructionThe allocation number was: 339855The object was allocated by thread 0x290, and the stack trace (return addresses) at the time was:
402DC0 [jpeg][jpeg][@GetMem]
404F15 [System][System][@NewAnsiString]
4054C9 [System][System][@LStrSetLength]
40512E [System][System][@LStrCat]
45C197 [StdCtrls][StdCtrls][TMemoStrings.Insert]
4200FA [Classes][Classes][TStrings.AddObject]
420180 [Classes][Classes][TStrings.AddStrings]
420232 [Classes][Classes][TStrings.Assign]
49175D [uSession.pas][uSession][TfraSession.AddLog][74]
4C3623 [uMain.pas][uMain][TfrmMain.CommLog][453]
4C3B71 [uMain.pas][uMain][TfrmMain._WM_CUSTOM_LOG][566]The object was subsequently freed by thread 0x290, and the stack trace (return addresses) at the time was:
402DEB [jpeg][jpeg][@FreeMem]
404E61 [System][System][@LStrClr]
45C22A [StdCtrls][StdCtrls][TMemoStrings.Insert]
4200FA [Classes][Classes][TStrings.AddObject]
420180 [Classes][Classes][TStrings.AddStrings]
420232 [Classes][Classes][TStrings.Assign]
49175D [uSession.pas][uSession][TfraSession.AddLog][74]
4C3623 [uMain.pas][uMain][TfrmMain.CommLog][453]
4C3B71 [uMain.pas][uMain][TfrmMain._WM_CUSTOM_LOG][566]
468C9B [Controls][Controls][TControl.WndProc]
4A12A6 [uComView.pas][uComView][TfraComView.PortOnRxChar][285]The current thread ID is 0x105C, and the stack trace (return addresses) leading to this error is:
4043AA [System][System][@AfterConstruction]
48F216 [ScktComp][ScktComp][TServerClientThread.Create]
4C2DE1 [uMain.pas][uMain][TfrmMain.SerSktThr][254]
48F078 [ScktComp][ScktComp][TServerWinSocket.GetServerThread]
48ECA4 [ScktComp][ScktComp][TServerWinSocket.Accept]
48F1AA [ScktComp][ScktComp][TServerAcceptThread.Execute]
42771F [Classes][Classes][ThreadProc]
404DE6 [System][System][ThreadWrapper]
7C80B729 [Unknown function at GetModuleFileNameA]Current memory dump of 256 bytes starting at pointer address 7FC96AB0:
8C BC 4C 00 80 80 80 80 80 80 80 80 80 80 00 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 70 83 44 7A
80 80 80 80 80 80 80 80 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
44 2F 05 00 A2 A9 40 00 13 2E 40 00 B3 54 40 00 04 52 40 00 E6 01 49 00 9C D8 4A 00 3D 84 94 7C
6F 84 94 7C 8A 11 92 7C 6B 5C 95 7C 8F 9A 93 7C 94 10 00 00 25 AA 40 00 13 2E 40 00 B3 54 40 00
04 52 40 00 E6 01 49 00 9C D8 4A 00 3D 84 94 7C 6F 84 94 7C 8A 11 92 7C 6B 5C 95 7C 8F 9A 93 7C
94 10 00 00 63 00 00 00 01 00 00 00 AF 90 C1 60 8C BC 4C 00 80 80 80 80 80 80 80 80 80 80 80 80
? ? L  .  €  €  €  €  €  €  €  €  €  €  .  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €
€  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €
€  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  €  p  ? D  z
€  €  €  €  €  €  €  €  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
D  /  .  .  ? ? @  .  .  .  @  .  ? T  @  .  .  R  @  .  ? .  I  .  ? ? J  .  =  ? ? |
o  ? ? |  ? .  ? |  k  \  ? |  ? ? ? |  ? .  .  .  %  ? @  .  .  .  @  .  ? T  @  .
.  R  @  .  ? .  I  .  ? ? J  .  =  ? ? |  o  ? ? |  ? .  ? |  k  \  ? |  ? ? ? |
? .  .  .  c  .  .  .  .  .  .  .  ? ? ? `  ? ? L  .  €  €  €  €  €  €  €  €  €  €  €  €

解决方案 »

  1.   

    主线程结束的时候尝试调用了一个已经free过的object,这个object是在线程里面创建的,好像是一个jpeg类型的对象。话说。。为啥服务端要操作UI,线程同步会造成很大的消耗的,严重影响服务端效率~
      

  2.   

    以下是相关代码://-----------------------------------------------------
    uMain.pastype
      TfrmMain = class(TForm)
        ...
      end;var
      frmMain: TfrmMain;implementationprocedure TfrmMain.SerSktThr(Sender: TObject; ClientSocket: TServerClientWinSocket;
      var SocketThread: TServerClientThread);
    begin
      SocketThread := TClientBlockThr.Create(false, ClientSocket);
    end;procedure TfrmMain.SktClientError(Sender: TObject; Socket: TCustomWinSocket;
      ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    begin
      ErrorCode := 0;
    end;//创建ServerSocket
    procedure TfrmMain.CreateSrvSocket;
    begin
      FSrvSocket := TServerSocket.Create(Self);
      with FSrvSocket do
      begin
        ServerType := stThreadBlocking;
        OnClientError := SktClientError;
        OnGetThread := SerSktThr;
      end;
    end;
    //-----------------------------------------------------------
    uSession.pastype
      TfraSession = class(TFrame)var
      fraSession: TfraSession;implementation//记录并显示日志,为防止过快刷屏采用先保存在TStrings
    procedure TfraSession.AddLog(S: string; bShow: Boolean);
    var
      vTimePoint: Cardinal;
    begin
      if FLogList.Count >= gConfig.MaxDispRows then
        FLogList.Clear;  //.Delete(0);  if Assigned(FLogList) then
        FLogList.Add(uLog_LogText(S));  if Self.Visible and (pctrl.ActivePage = tsLog) then  //窗体可见,且活动页为日志页
      begin
        vTimePoint := getTickCount;
        if vTimePoint - gVCLLogTimePoint > C_VCL_LOG_INTERVAL then
        begin
          mmLog.Lines.Assign(FLogList);
          //mmLog.Perform(WM_VSCROLL,SB_BOTTOM,0);    //滚动条滚动到最底行
          gVCLLogTimePoint := vTimePoint;
        end;
      end;  if bShow and Self.Visible then pctrl.ActivePage := tsLog;
    end;
      

  3.   

    它说我有问题的2个单元我都没有引用JPEG,JPEG只是我的一个欢迎界面有用到显示一张JPG图片,如下:unit uMain;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Controls, Forms,
      Dialogs, Menus, ComCtrls, ExtCtrls, StdCtrls, Buttons, uExComponents,
      Graphics, ScktComp, ShellAPI,  uConst, uSession, uAutoTopUpThr, CoolTrayIcon;//------------------------------------------------------------------------------unit uSession;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
      Dialogs, ComCtrls, StdCtrls, ImgList, uExComponents;