...
Query: TSQLQuery
...
Query.SQL.Clear;
Query.SQL.Add('INSERT INTO pumps'#13#10 +
'VALUES (NULL, :title, :power)');
Query.ParamByName('title').AsString:= title;
Query.ParamByName('power').AsFloat:= power;
try
Query.ExecSQL;
except
on E: Exception do
begin
ShowMessage(E.Message);
end;
end;
使用driver = Sqlite将查询链接到SQLConnection。所有操作都可以正常工作,但是当我尝试将非唯一值插入到具有唯一索引的列中时,我会得到有关值存在的异常。好的,我的代码捕获它并显示消息'列标题不唯一',但在调试器显示异常TWICE之前。当我关闭程序时,内存管理器会告诉内存泄漏,泄漏是:
有可能避免这种情况吗?
UPD:我使用FastMM4获取详细日志,下面是关于一次内存泄漏的部分:
A memory block has been leaked. The size is: 68
This block was allocated by thread 0xB18, and the stack trace (return addresses) at the time was:
4068A6 [System.pas][System][@GetMem$qqri][4203]
4082BB [System.pas][System][TObject.NewInstance$qqrv][14969]
4089D2 [System.pas][System][@ClassCreate$qqrpvzc][16231]
7D5A91 [Data.DbxSqlite.pas][Data.DbxSqlite][Dbxsqlite.TDBXSqliteCommand.$bctr$qqrp26Data.Dbxcommon.TDBXContextp35Data.Dbxsqlite.TDBXSqliteConnection][567]
6E3DFE [Data.DBXCommon][Generics.Collections.%TDictionary__2$20System.UnicodeStringp32Data.Dbxcommon.TDBXCreateCommand%.GetBucketIndex$qqrx20System.UnicodeStringi]
7D4C8E [Data.DbxSqlite.pas][Data.DbxSqlite][Dbxsqlite.TDBXSqliteDriver.CreateSqliteCommand$qqrp26Data.Dbxcommon.TDBXContextp29Data.Dbxcommon.TDBXConnectionp26Data.Dbxcommon.TDBXCommand][295]
6CE4DA [Data.DBXCommon.pas][Data.DBXCommon][Dbxcommon.TDBXDriver.CreateMorphCommand$qqrp26Data.Dbxcommon.TDBXContextp29Data.Dbxcommon.TDBXConnectionp26Data.Dbxcommon.TDBXCommand][7569]
6D5177 [Data.DBXCommon.pas][Data.DBXCommon][Dbxcommon.TDBXDelegateDriver.CreateMorphCommand$qqrp26Data.Dbxcommon.TDBXContextp29Data.Dbxcommon.TDBXConnectionp26Data.Dbxcommon.TDBXCommand][11061]
6D09F4 [Data.DBXCommon.pas][Data.DBXCommon][Dbxcommon.TDBXConnection.CreateMorphCommand$qqrp26Data.Dbxcommon.TDBXCommand][8480]
6D1C21 [Data.DBXCommon.pas][Data.DBXCommon][Dbxcommon.TDBXMorphicCommand.DerivedOpen$qqrv][9084]
6D1974 [Data.DBXCommon.pas][Data.DBXCommon][Dbxcommon.TDBXCommand.Open$qqrv][8962]
The block is currently used for an object of class: TDBXSqliteCommand
答案 0 :(得分:3)
我使用dbXpress SQLite 和 MySQL 驱动程序(XE3)运行测试,并且您的代码只是在SQLite中泄漏(在调试模式下也获得双重异常)但它不适用于MySQL 。
恕我直言,这是一个应该向QC报告的错误。
在调试器中第一次出现异常时堆栈跟踪
:769cc41f KERNELBASE.RaiseException + 0x58
Data.DBXCommon.TDBXContext.Error(???,'column title is not unique')
Data.DbxSqlite.CheckError(19,???,$2F12738)
Data.DbxSqlite.TDBXSqliteCommand.DerivedExecuteQuery
Data.DBXCommon.TDBXCommand.ExecuteQuery
Data.DBXCommon.TDBXMorphicCommand.ExecuteQuery
Data.SqlExpr.TCustomSQLDataSet.ExecuteStatement
Data.SqlExpr.TCustomSQLDataSet.ExecSQL(???)
Data.SqlExpr.TSQLQuery.ExecSQL(???)
Main_ViewU.TForm1.RunQuery($2E7B870,'Title',10)
Main_ViewU.TForm1.Button1Click($2E85AD0)
在调试器中第二次异常发生时堆栈跟踪
:769cc41f KERNELBASE.RaiseException + 0x58 // <-- Exception interrupts Destroy
Data.DBXCommon.TDBXContext.Error(???,'column title is not unique')
Data.DbxSqlite.CheckError(19,???,$2F12738)
Data.DbxSqlite.TDBXSqliteCommand.DerivedClose
Data.DBXCommon.TDBXCommand.Close
Data.DBXCommon.TDBXMorphicCommand.DerivedClose
Data.DBXCommon.TDBXCommand.Close
Data.DBXCommon.TDBXCommand.Destroy // <-- DESTROY
Data.DBXCommon.TDBXMorphicCommand.Destroy // <-- DESTROY
System.TObject.Free
Data.SqlExpr.TCustomSQLDataSet.CloseStatement
Data.SqlExpr.TCustomSQLDataSet.InternalFreeCommand
Data.SqlExpr.TCustomSQLDataSet.FreeCommand
Data.SqlExpr.TCustomSQLDataSet.ExecSQL(???)
Data.SqlExpr.TSQLQuery.ExecSQL(???)
Main_ViewU.TForm1.RunQuery($2E7B870,'Title',10)
Main_ViewU.TForm1.Button1Click($2E85AD0)
这会导致内存泄漏......