Delphi

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • compiler warning

    5 answers - 1468 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Hello,
    I have a compiler warning in following code 'F might not be
    initialized', at the M.LoadFromStream(F); line.
    M := TMemoryStream.Create;
    try
    for n := 0 to FFiles.Count - 1 do begin
    try
    F := TFileStream.Create(FFiles[n], Read or fmShareDenyWrite);
    except
    TriggerWarning('Could not open ''' + FFiles[n] + '''');
    Continue;
    end;
    M.LoadFromStream(F);
    F.Free;
    if not DeleteFile(FFiles[n]) then
    TriggerWarning('Cannot delete file ''' + FFiles[n] + ''', will not transmit it.')
    else
    // Todo: add to database and transmit it
    end;
    finally
    M.Free;
    end;
    However unless my brain is damaged due to late hours work, it should
    always be initialized because if exception occure I 'continue' the loop.
    The TriggerWarning fires an event (yeah I know DoWarning is be more
    used), and FFiles is a TStringList containing filenames in a given
    folder.
    What I'm trying to do is read files generated by another application,
    open them, do something with it and delete it. The other application may
    not change the file after I open it of course.
    Can I make the compiler happy, or make some better code to etablish this
    ?
    Rgds, Wilfried
    http://www.mestdagh.biz
    Delphi-Talk mailing list -Delphi-Talk (AT) elists (DOT) org
  • No.1 | | 604 bytes | |

    Hello,

    I changed like this to make the compiler happy:

    M := TMemoryStream.Create;
    try
    for n := 0 to FFiles.Count - 1 do begin
    try
    F := TFileStream.Create(FFiles[n], Read or fmShareDenyWrite);
    except
    TriggerWarning('Could not open ''' + FFiles[n] + '''');
    F := nil;
    end;
    if not Assigned(F) then
    Continue;
    M.LoadFromStream(F);

    But still I like to know wy the warning :(

    Rgds, Wilfried
    http://www.mestdagh.biz

    Delphi-Talk mailing list -Delphi-Talk (AT) elists (DOT) org
  • No.2 | | 2091 bytes | |

    Wilfried Mestdagh wrote:
    Hello,

    I have a compiler warning in following code 'F might not be
    initialized', at the M.LoadFromStream(F); line.

    M := TMemoryStream.Create;
    try
    for n := 0 to FFiles.Count - 1 do begin
    try
    F := TFileStream.Create(FFiles[n], Read or fmShareDenyWrite);
    except
    TriggerWarning('Could not open ''' + FFiles[n] + '''');
    Continue;
    end;
    M.LoadFromStream(F);
    F.Free;

    All the above can be replaced with this:

    try
    M.LoadFromFile(FFiles[n]);
    except
    on EFileStreamError do begin
    TriggerWarning();
    continue;
    end;
    end;

    (The exact exception class might not be accurate for all versions of
    Delphi. I don't remember EFileStreamError from Delphi 5, but it's in
    Delphi 2005.)

    if not DeleteFile(FFiles[n]) then
    TriggerWarning('Cannot delete file ''' + FFiles[n] + ''', will not transmit it.')
    else
    // Todo: add to database and transmit it
    end;
    finally
    M.Free;
    end;

    However unless my brain is damaged due to late hours work, it should
    always be initialized because if exception occure I 'continue' the loop.

    The compiler sometimes has trouble following execution paths into or out
    of "except" blocks for the purpose of detecting usage of variables.

    What I'm trying to do is read files generated by another application,
    open them, do something with it and delete it. The other application may
    not change the file after I open it of course.

    You should take a look at the File_Flag_DClose flag, which you
    can pass to CreateFile when you open a file. You can keep the file open
    as long as you want, and when you close it, the S will delete it for
    you. It makes sure no one else opens it without also specifying "delete"
    permission. Instead of using a TFileStream or a TMemoryStream, you would
    need to call CreateFile directly, and then perhaps use the returned
    handle in a THandleStream.
  • No.3 | | 1972 bytes | |

    This warning appears, because F can be equal to nil if TFileStream.Create
    raises exception. In this case youll handle exception, but not initialize
    your variable F.
    Sorry for my poor english :)

    Message
    From: "Wilfried Mestdagh" <wilfried (AT) mestdagh (DOT) biz>
    To: "'Delphi-Talk Discussion List'" <delphi-talk (AT) elists (DOT) org>
    Sent: Sunday, 23, 2005 9:28 PM
    Subject: compiler warning

    Hello,

    I have a compiler warning in following code 'F might not be
    initialized', at the M.LoadFromStream(F); line.

    M := TMemoryStream.Create;
    try
    for n := 0 to FFiles.Count - 1 do begin
    try
    F := TFileStream.Create(FFiles[n], Read or
    fmShareDenyWrite);
    except
    TriggerWarning('Could not open ''' + FFiles[n] + '''');
    Continue;
    end;
    M.LoadFromStream(F);
    F.Free;
    if not DeleteFile(FFiles[n]) then
    TriggerWarning('Cannot delete file ''' + FFiles[n] + ''', will not
    transmit it.')
    else
    // Todo: add to database and transmit it
    end;
    finally
    M.Free;
    end;

    However unless my brain is damaged due to late hours work, it should
    always be initialized because if exception occure I 'continue' the loop.

    The TriggerWarning fires an event (yeah I know DoWarning is be more
    used), and FFiles is a TStringList containing filenames in a given
    folder.

    What I'm trying to do is read files generated by another application,
    open them, do something with it and delete it. The other application may
    not change the file after I open it of course.

    Can I make the compiler happy, or make some better code to etablish this
    ?

    Rgds, Wilfried
    http://www.mestdagh.biz

    Delphi-Talk mailing list -Delphi-Talk (AT) elists (DOT) org

    Delphi-Talk mailing list -Delphi-Talk (AT) elists (DOT) org
  • No.4 | | 421 bytes | |

    Hello Rob,

    You should take a look at the File_Flag_DClose flag, which you
    can pass to CreateFile when you open a file. You can keep the file open
    as long as you want, and when you close it, the S will delete it for

    This was exacly what I needed, thx ! Works as a glance.

    Rgds, Wilfried
    http://www.mestdagh.biz

    Delphi-Talk mailing list -Delphi-Talk (AT) elists (DOT) org
  • No.5 | | 464 bytes | |

    Rustamas Narusevicius wrote:
    This warning appears, because F can be equal to nil if TFileStream.Create
    raises exception. In this case youll handle exception, but not initialize
    your variable F.

    If TFileStream.Create raises an exception, then F will remain
    uninitialized. However, the "except" clause catches the exception and uses
    "Continue" to skip the rest of the body of the loop, so F never gets used,
    either. The warning is incorrect.

Re: compiler warning


max 4000 letters.
Your nickname that display:
In order to stop the spam: 9 + 9 =
QUESTION ON "Delphi"

EMSDN.COM