var
tempbmp:tbitmap;
begin tempbmp:=tbitmap.create;end;我想在 tempbmp:=tbitmap.create; 之前先判断一下tempbmp是否已经create了,不知到怎么判断啊!
tempbmp:tbitmap;
begin tempbmp:=tbitmap.create;end;我想在 tempbmp:=tbitmap.create; 之前先判断一下tempbmp是否已经create了,不知到怎么判断啊!
解决方案 »
- 急急急,关于获取别的进程的EProcess
- GDI+路径保存线帽问题
- 扩展ListView遇到的几个问题,请大家指点一下!
- 如何识别一个数据库中是否存在某个表?
- 关于Delphi6的补丁问题、?
- ServerSocket的线程方式
- #### HELP **** 字体的问题??****分数多多****
- 如何改变DBGrid网格颜色,用到什么事件?除了OnDrawColumnCell和onGetCellParam还有别的事件吗
- 无知的问题:好的多层的客户端程序一般是什么样儿的?
- 请问怎样把数据库的IMAGE字段的图片在IMAGE控件上显示出来?
- 求神龙卡V代或新一代osd叠加程序
- FFmpeg支持的最高码率
释放的时候nil之
tempbmp:tbitmap;
begin
if not assigned(tempbmp) then
begin
tempbmp:=tbitmap.create;
try
...
finally
freeAndNil(tempbmp);
end;
end;end;
if tempbmp<> nil then //不为空说明已经 create了
begin
//
end;最后释放时候 tempbmp.free
正好最近我也在做tbitmap的应用
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; 这句如果注释掉的话和不注释掉,后面的代码运行效果是相同的!
判断是否create 用
tempbmp :=nil ;//先置空
tempbmp:=tbitmap.Create; //再 create
if tempbmp <> nil then //不为空说明已经 create了
begin
//
end; 最后释放时候
tempbmp.free ;
tempbmp :=nil //再置空看看 ok 不
楼上的已经可以了,但是在具体问题中还是不行,我遇到的问题是这样的:
一个抓图的函数,然后点击按钮会重复的调用抓图函数,多次点击按钮后就会出现内存持续增长的问题,之后出错!代码:
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建立后没有及时释放,运行时候,内存一直增长,这个问题一直没解决,帮忙看看啊
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;
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.
我测试了不行,楼主你行吗?
tempbmp为局部变量,还没 create 就 试图free就显然不对的,虽然这句并不执行。剩下的 就是 10楼说的了,用到的已经create的变量必须要 free,函数返回值也是一样,在调用的函数/过程中必须用完释放。(在Button5Click中不需create)
否则内存自然不断增长。按十楼那样可以 或者
bmp := snap(0,0,100,100);
try
image1.picture.bitmap.assign(bmp);
finally
bmp.free;
end;
tempbmp:tbitmap;
begin
tempbmp := nil;
if not assigned(tempbmp) then
begin
tempbmp:=tbitmap.create;
try
...
finally
freeAndNil(tempbmp);
end;
end;end;