var
 tempbmp:tbitmap;
begin tempbmp:=tbitmap.create;end;我想在 tempbmp:=tbitmap.create; 之前先判断一下tempbmp是否已经create了,不知到怎么判断啊!

解决方案 »

  1.   

    assigned判断
    释放的时候nil之
      

  2.   

    var 
    tempbmp:tbitmap; 
    begin 
      if not assigned(tempbmp) then
      begin
        tempbmp:=tbitmap.create; 
        try
         ...
        finally
         freeAndNil(tempbmp);
        end;
    end;end;
      

  3.   

    判断是否create 用 
    if tempbmp<> nil then  //不为空说明已经 create了
    begin
    //
    end;最后释放时候 tempbmp.free
    正好最近我也在做tbitmap的应用
      

  4.   

    拜托你们回答的时候测试下好吗?
    if not assigned(tempbmp) then 

    if tempbmp <> nil then  
    都不能判断出tempbmp是否create
    你们都测试没有啊?!
    代码:
    procedure TForm1.Button5Click(Sender: TObject);
    var
    tempbmp:tbitmap;
    begin
    //tempbmp:=tbitmap.Create;
    if tempbmp<>nil then showmessage('0') ;
    if Assigned(tempbmp) then showmessage('.');end;
    这段代码,tempbmp:=tbitmap.Create; 这句如果注释掉的话和不注释掉,后面的代码运行效果是相同的!
      

  5.   

    虽然楼主态度不好,但我还是给你个回复
    判断是否create 用 
    tempbmp :=nil ;//先置空
    tempbmp:=tbitmap.Create; //再 create
    if tempbmp <> nil then  //不为空说明已经 create了 
    begin 
    // 
    end; 最后释放时候 
    tempbmp.free ;
    tempbmp :=nil //再置空看看 ok 不
      

  6.   

    不好意思啊,因为着急,所以态度不好,请原谅啊!
    楼上的已经可以了,但是在具体问题中还是不行,我遇到的问题是这样的:
    一个抓图的函数,然后点击按钮会重复的调用抓图函数,多次点击按钮后就会出现内存持续增长的问题,之后出错!代码:
    function snap(Left,Top,Right,Bottom:integer):tbitmap;
    var
      dc: HDC;
      rWidth:integer;
      rHeight:integer;
      tempbmp:tbitmap;
    begin
      rWidth := Right - Left;
      rHeight := Bottom - Top;
      dc := GetDC(hwnd_desktop);
      if tempbmp=nil then tempbmp.Free;
      tempbmp:=nil;
      tempbmp := TBitmap.create;
      try
        tempbmp.Width := rwidth;
        tempbmp.Height := rheight;
        BitBlt(tempbmp.Canvas.Handle, 0,0,rwidth,rheight,
          dc, left, top, SRCCOPY);
      finally
        ReleaseDC(HWND_DESKTOP, dc);
        dc:=0;
      end;
        result:=tempbmp;
    end;procedure TForm1.Button5Click(Sender: TObject); 
    var 
    i:integer; 
    begin
    for i:=0 to 100000 do begin 
     image1.picture.bitmap.assign(snap(0,0,100,100));
    end;
    end; 
    这样的程序,因为在抓图snap中的tempbmp建立后没有及时释放,运行时候,内存一直增长,这个问题一直没解决,帮忙看看啊
      

  7.   

    函数返回值是需要外部释放的:
    procedure TForm1.Button5Click(Sender: TObject); 
    var 
    i:integer;
    bmp: tbitmap; 
    begin 
    for i:=0 to 100000 do 
    begin 
      bmp := snap(0,0,100,100);
      image1.picture.bitmap.assign(bmp);
      bmp.free;  
    end;
    end; 
      

  8.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Image1: TImage;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
     {function snap(Left,Top,Right,Bottom:integer):tbitmap;
    var 
      dc: HDC; 
      rWidth:integer; 
      rHeight:integer; 
      tempbmp:tbitmap; 
    begin 
      rWidth := Right - Left;
      rHeight := Bottom - Top; 
      dc := GetDC(hwnd_desktop);
      tempbmp:=nil;
      if tempbmp<>nil then tempbmp.Free;
    //
      tempbmp := TBitmap.create; 
      try 
        tempbmp.Width := rwidth; 
        tempbmp.Height := rheight; 
        BitBlt(tempbmp.Canvas.Handle, 0,0,rwidth,rheight, 
          dc, left, top, SRCCOPY); 
      finally 
        ReleaseDC(HWND_DESKTOP, dc); 
        //dc:=0;
      end;
        result:=tempbmp;
    end;}
     function snap(Left,Top,Right,Bottom:integer):tbitmap; 
    var 
      dc: HDC; 
      rWidth:integer; 
      rHeight:integer; 
      tempbmp:tbitmap; 
    begin 
      rWidth := Right - Left; 
      rHeight := Bottom - Top; 
      dc := GetDC(hwnd_desktop); 
      if tempbmp=nil then tempbmp.Free; 
      tempbmp:=nil;
      tempbmp := TBitmap.create;
      try 
        tempbmp.Width := rwidth;
        tempbmp.Height := rheight;
        BitBlt(tempbmp.Canvas.Handle, 0,0,rwidth,rheight, 
          dc, left, top, SRCCOPY); 
      finally 
        ReleaseDC(HWND_DESKTOP, dc); 
        dc:=0; 
      end; 
        result:=tempbmp; 
    end; procedure TForm1.Button1Click(Sender: TObject);
    var 
    i:integer; 
    bmp: tbitmap; 
    begin 
    for i:=0 to 100000 do 
    begin 
      bmp := snap(0,0,100,100); 
      image1.picture.bitmap.assign(bmp); 
      bmp.free;  
    end; 
    end;
    end.
    我测试了不行,楼主你行吗?
      

  9.   

    首先程序中错误 if tempbmp=nil then tempbmp.Free; 
    tempbmp为局部变量,还没 create 就 试图free就显然不对的,虽然这句并不执行。剩下的 就是 10楼说的了,用到的已经create的变量必须要 free,函数返回值也是一样,在调用的函数/过程中必须用完释放。(在Button5Click中不需create)
    否则内存自然不断增长。按十楼那样可以 或者
      bmp := snap(0,0,100,100); 
      try
        image1.picture.bitmap.assign(bmp); 
      finally
        bmp.free; 
      end; 
      

  10.   

    var
    tempbmp:tbitmap;
    begin
      tempbmp := nil;
      if not assigned(tempbmp) then
      begin
        tempbmp:=tbitmap.create;
        try
          ...
        finally
          freeAndNil(tempbmp);
        end;
    end;end;