我动态创建了一个dbgrid对象,当用free释放时,编译出错信息为:
[Warning] unit3.pas(78): Variable 'wtgd' might not have been initialized
部分代码如下:
 try
    wtgd:=tdbgrid.Create(nil);
    wtgd.DataSource:=datamodule1.DataSource5;
 finally
    wtgd.Free;
  end;请各位高手指点,谢谢!

解决方案 »

  1.   

    [Warning] unit3.pas(78): Variable 'wtgd' might not have been initialized
    这是个警告信息,说你的dbgrid没有初始化,不用管它的。
      

  2.   

    但如果没有wtgrid.free这句就没有错误的,是什么原因呢?
      

  3.   

    我想你误解我的意思了,当创建后没有释放那条语句,是没有任何出错信息的,而加上后就有上面的错误。难道是因为create创建对象,会自动释放吗?
      

  4.   

    DELPHI不会自动释放的。。
    这样写应该就没有了。
     wtgd:=tdbgrid.Create(nil);
     try
        
        wtgd.DataSource:=datamodule1.DataSource5;
     finally
        wtgd.Free;
      end;
      

  5.   

    Sometimes you want to ensure that specific parts of an operation are completed, whether or not the operation is interrupted by an exception. For example, when a routine acquires control of a resource, it is often important that the resource be released, regardless of whether the routine terminates normally. In these situations, you can use a try...finally statement.The following example shows how code that opens and processes a file can ensure that the file is ultimately closed, even if an error occurs during execution.Reset(F);
    try
     ...  // process file F
    finally
      CloseFile(F);
    end;The syntax of a try...finally statement istry statementList1 finally statementList2 endwhere each statementList is a sequence of statements delimited by semicolons. The try...finally statement executes the statements in statementList1 (the try clause). If statementList1 finishes without raising exceptions, statementList2 (the finally clause) is executed. If an exception is raised during execution of statementList1, control is transferred to statementList2; once statementList2 finishes executing, the exception is re-raised. If a call to the Exit, Break, or Continue procedure causes control to leave statementList1, statementList2 is automatically executed. Thus the finally clause is always executed, regardless of how the try clause terminates.If an exception is raised but not handled in the finally clause, that exception is propagated out of the try...finally statement, and any exception already raised in the try clause is lost. The 
    finally clause should therefore handle all locally raised exceptions, so as not to disturb propagation of other exceptions.