adoquery 释放报错(100分)

M

msglzcx

Unregistered / Unconfirmed
GUEST, unregistred user!
library TEST;<br><br>uses<br>&nbsp; adodb,db,ActiveX;<br><br>var<br>&nbsp; AdoCon: TADOConnection;<br>&nbsp; AdoQry: TADOQuery;<br><br>{$R *.res}<br><br>procedure DllExit(Reason: integer);<br>begin<br>&nbsp; case Reason of<br>&nbsp; &nbsp; 1 : begin<br>&nbsp; &nbsp; end; &nbsp;<br>&nbsp; &nbsp; 0 : begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AdoQry.Close;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AdoQry.Free; //这里报错 用AdoQry := nil;就没事<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//AdoCon.Close;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//AdoCon.Free;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CoUninitialize;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br>procedure LoadShow(aInput: pchar;var aOutPut: pchar);stdcall;<br>begin<br>&nbsp; AdoQry.Close;<br>&nbsp; AdoQry.SQL.Clear;<br>&nbsp; AdoQry.SQL.Add('declare @out varchar(1000)');<br>&nbsp; AdoQry.SQL.Add('EXEC RRC_TEST '''+ aInput +''',@out output');<br>&nbsp; AdoQry.SQL.Add('select @out as Out');<br>&nbsp; AdoQry.Open;<br>&nbsp; aOutPut := PChar(AdoQry.Fields[0].Text);<br>end;<br><br>exports<br>&nbsp; LoadShow;<br><br>begin<br>&nbsp; CoInitialize(nil);<br>&nbsp; AdoCon := TADOConnection.Create(nil);<br>&nbsp; AdoQry := TADOQuery.Create(nil);<br>&nbsp; try<br>&nbsp; &nbsp; AdoCon.ConnectionString := 'Provider=SQLOLEDB.1;Password=;Persist Security Info=True;User ID=sa;Initial Catalog=rainrest;Data Source=10.1.18.56';<br>&nbsp; &nbsp; AdoCon.LoginPrompt := False;<br>&nbsp; &nbsp; AdoCon.Connected := True;<br>&nbsp; &nbsp; AdoQry.Connection := AdoCon;<br>&nbsp; except<br>&nbsp; &nbsp; AdoQry.Free;<br>&nbsp; &nbsp; AdoCon.Free;<br>&nbsp; end;<br>&nbsp; DllProc := @DllExit;<br>&nbsp; DllExit(1);<br>end.
 
触发异常时AdoQry.Free;<br>然后退用过程时DllExit如果参数是0 则又释放一次,当然要报错了。
 
呵呵<br><br>&nbsp; &nbsp;try<br>...<br>&nbsp; except<br>&nbsp; &nbsp; AdoQry.Free; //这里释放,退出又释放<br>&nbsp; &nbsp; AdoQry := nil;//<br>&nbsp; &nbsp; AdoCon.Free;<br>&nbsp; end;<br><br>procedure DllExit(Reason: integer);<br>begin<br>&nbsp; case Reason of<br>&nbsp; &nbsp; 1 : begin<br>&nbsp; &nbsp; end; &nbsp;<br>&nbsp; &nbsp; 0 : begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if AdoQry&lt;&gt; nil then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AdoQry.Close;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AdoQry.Free; //这里报错 用AdoQry := nil;就没事<br>&nbsp; &nbsp; &nbsp; &nbsp;end;
 
异常没有出发,原因是我调用的时候用得静态调用,谢谢大家的回答
 
这里异常处理跟调用没关系的,<br><br>这段内容写错 'Provider=SQLOLEDB.1;Password=;Persist Security Info=True;User ID=sa;Initial Catalog=rainrest;Data Source=10.1.18.56'<br><br>执行这里 AdoCon.Connected := True;就会有异常。<br><br>不过无论如何,你原来的写法都是错的,因为万一异常的时候,释放了 AdoQry,退出的时候,不加判断,就会再次释放,所以报错,很多时候莫名其妙的东西就是这样来了。
 
两次释放的原因。
 
楼主用dll 我最近也做过 直接adoquery.free 就行了 我都可以 不过就是一个关于数据加载到树就不行了 调用第一次退出窗口正常 再打开窗口退出时就出错了
 
既然是全局的变量,就在开始时创建,结束时释放就可以了,文中却有2个地方释放。<br>要不在释放之前判断就ok了:<br>If AdoQry &lt;&gt; Nil Then AdoQry.free;
 
楼主可以加我QQ &nbsp;我也正碰到这类问题 &nbsp; QQ:271160724
 
顶部