1.TList,TStrings,TStringList调用Delete方法后,并不释放内存,只有调用~~.Capacity := ~~.Count;后才释放内存。2.Form1 := TForm1.Create(Application);
Form1.ShowModal;
未释放内存,应用try
Form1.ShowModal;
finally
Form1.Free;
end;
Form1.ShowModal;
未释放内存,应用try
Form1.ShowModal;
finally
Form1.Free;
end;
var
nIndex : Integer ;
begin
//错误,随着删除的继续,ltData里的项目数在不断减少,
//而循环体的上限没有跟着变化,最后发生数组越界错误。
for nIndex := 0 to ltData.Count - 1 do
ltData.Delete(nIndex) ;
end ;改正的方法:1不用for循环
2
var
nIndex : Integer ;
begin
//错误,随着删除的继续,ltData里的项目数在不断减少,
//而循环体的上限没有跟着变化,最后发生数组越界错误。
for nIndex := 0 to ltData.Count - 1 do
ltData.Delete(0) ;
end ;3
var
nIndex : Integer ;
begin
//错误,随着删除的继续,ltData里的项目数在不断减少,
//而循环体的上限没有跟着变化,最后发生数组越界错误。
for nIndex := ltData.Count - 1 downto 0 do
ltData.Delete(nIndex) ;
end ;
var
nIndex : Integer ;
begin
//错误,随着删除的继续,ltData里的项目数在不断减少,
//而循环体的上限没有跟着变化,最后发生数组越界错误。
for nIndex := 0 to ltData.Count - 1 do
ltData.Delete(nIndex) ;
end ;改正的方法:1不用for循环
2
var
nIndex : Integer ;
begin
for nIndex := 0 to ltData.Count - 1 do
ltData.Delete(0) ;
end ;3
var
nIndex : Integer ;
begin
for nIndex := ltData.Count - 1 downto 0 do
ltData.Delete(nIndex) ;
end ;
while not table1.eof do
begin
...
table1.next; //忘了加next;
end;
var
astr: String;
begin
astr := IntToStr(Val);
Result := astr;//astr在退出函数时会被释放,而返回值却指向它的内容...
end;
使用New分配的内存必须用Dispose释放.
New可以自动计算需分配的空间长度,适用于给一个Record指针分配空间,没有AllocMem和StrAlloc好用.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;type
TForm1 = class(TForm)
procedure FormDblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}
uses Unit2; //
...unit Unit2;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;type
TForm2 = class(TForm)
procedure FormDblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form2: TForm2;implementation{$R *.DFM}
uses unit1; //循环引用
...
DELPHI 5+ADO请下载补丁
Win2000下改变窗体的字体,Win98下变大
建议动态设置字体
if (1=1) and (2=2) then //正确///////////////////////if flag then
...;//错误,有else从句不能加";"
else
... ;if flag then
... //正确
else
... ;
begin
Form1.Caption := '在类方法中使用类的实例'; //Self.Caption := '应该是';
end;
1. 声明的函数名大小写不区分
//library中
function myfunc(); stdcall;
//unit中
function MyFunc(); stdcall;
2. 使用string做参数处理
function MyFunc(mStr: string); stdcall;
3. 使用stdcall指示不统一
//library中
function MyFunc(); stdcall;
//unit中
function MyFunc();
1. 声明的函数名大小写不区分
//library中
function myfunc(); stdcall;
//unit中
function MyFunc(); stdcall;
2. 使用string做参数处理
function MyFunc(mStr: string); stdcall;
3. 使用stdcall指示不统一
//library中
function MyFunc(); stdcall;
//unit中
function MyFunc();
使用adoquery.append前没有判断数据集是否为空.
使用image控件动态显示jpeg图片的时候没有加入jpeg单元.
1.指针&pchar
例:
var
p:pchar;
begin
p:='aaa';
strcopy(p,'aaa');//错误,p尚未分配内存
new(p);//错误,new不能为pchar分配内存
//////////////////////////
getmem(p,255*sizeof(char));//正确,分配了一定数量的内存;AllocMem可以把分配的内存初始化为0
strcopy(p,'aaa');
freemem(p);//如果动态分配的内存不显示释放,进程结束前该内存不会被释放,如果你的函数24*7的不断调用,你会咒骂Windows的不稳定
end;
--------------------------------------------------------------------------------------------------------------------------
2.开放(动态)数组
var
a:array of integer;
i:integer;
begin
i:=10;
setlength(a,i);
for i:=1 to 10 //错误,开放(动态)数组缺省是零基准数组
a[i]:=i;
for i:=Low(a) to High(a)
a[i]:=i;
end;
--------------------------------------------------------------------------------------------------------------------------
3.TTreeNode.Data
TTreeNode.Data属性是Pointer型,它不负责内存的申请和释放,为了你的程序的健壮性,检查一下内存的使用情况
--------------------------------------------------------------------------------------------------------------------------
4.TList,TStrings,TStringList调用Delete方法后,并不释放内存,只有调用~~.Capacity := ~~.Count;后才释放内存。
--------------------------------------------------------------------------------------------------------------------------
5.Form1 := TForm1.Create(Application);
Form1.ShowModal;
未释放内存,应用try
Form1.ShowModal;
finally
Form1.Free;
end;
--------------------------------------------------------------------------------------------------------------------------
6.假设ltData是一个数列型的数据(如TList,TStrings,TListItems)
var
nIndex : Integer ;
begin
//错误,随着删除的继续,ltData里的项目数在不断减少,
//而循环体的上限没有跟着变化,最后发生数组越界错误。
for nIndex := 0 to ltData.Count - 1 do
ltData.Delete(nIndex) ;
end ;改正的方法:
1不用for循环
或2
var
nIndex : Integer ;
begin
for nIndex := 0 to ltData.Count - 1 do
ltData.Delete(0) ;
end ;
或3
var
nIndex : Integer ;
begin
for nIndex := ltData.Count - 1 downto 0 do
ltData.Delete(nIndex) ;
end ;
--------------------------------------------------------------------------------------------------------------------------
7.
function IntToPchar(Val:Integer):PChar;
var
astr: String;
begin
astr := IntToStr(Val);
Result := astr;//astr在退出函数时会被释放,而返回值却指向它的内容...
end;
--------------------------------------------------------------------------------------------------------------------------
8.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;type
TForm1 = class(TForm)
procedure FormDblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}
uses Unit2; //
...unit Unit2;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;type
TForm2 = class(TForm)
procedure FormDblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form2: TForm2;implementation{$R *.DFM}
uses unit1; //循环引用
...
--------------------------------------------------------------------------------------------------------------------------
9.在 delphi6 下的数据库程序,请不要在 Delphi 关闭的情况下修改表的结构,否则你的程序就不好打开了-老是异常退出(千万不要再安装系统哦!)。
--------------------------------------------------------------------------------------------------------------------------
10.Win2000下改变窗体的字体,Win98下变大
建议动态设置字体
--------------------------------------------------------------------------------------------------------------------------
11.
if 1=1 and 2=2 then //错误and 高于=
if (1=1) and (2=2) then //正确///////////////////////if flag then
...;//错误,有else从句不能加";"
else
... ;if flag then
... //正确
else
... ;
--------------------------------------------------------------------------------------------------------------------------
12.
procedure TForm1.Button1Click(Sender: TObject);
begin
Form1.Caption := '在类方法中使用类的实例'; //Self.Caption := '应该是';
end;
--------------------------------------------------------------------------------------------------------------------------
13.使用动态连接库是容易犯
1. 声明的函数名大小写不区分
//library中
function myfunc(); stdcall;
//unit中
function MyFunc(); stdcall;
2. 使用string做参数处理
function MyFunc(mStr: string); stdcall;
3. 使用stdcall指示不统一
//library中
function MyFunc(); stdcall;
//unit中
function MyFunc();
--------------------------------------------------------------------------------------------------------------------------
14.使用adoquery.append前没有判断数据集是否为空.
使用image控件动态显示jpeg图片的时候没有加入jpeg单元.
只不过....我把C++ Builder 6打开了。
:)
通过delphi关闭,而应该继续运行,再通过应用程序关闭
function TStrings.IndexOf(const S: string): Integer;
begin
for Result := 0 to GetCount - 1 do //精煉
if CompareStrings(Get(Result), S) = 0 then Exit;
Result := -1;
end;
新column,否則會報'out of bound'錯誤.