Variable scope bug?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Todd

    #1

    Variable scope bug?

    I've got a problem with the scope of a variable, consider
    the following example:


    MyCOMObject.MyO bjectClass obj;

    try
    {
    obj = new MyCOMObject.MyO bjectClass();
    returnXml = obj.getEnquiryU sers();
    }
    catch (Exception e)
    {
    //handle error
    }
    finally
    {
    if (obj!=null)
    {
    Marshal.Release ComObject(obj);
    obj = null;
    }
    }

    This won't compile, it tells me that the variable obj is
    unassigned when I test if it's null in the finally
    statement.

    This doesn't seem correct to me; I need to release the
    COM object in the finally statement as if there is an
    error in the try block the object will never be released.
    These steps work in Java and are the steps we normally
    take when interfacing with COM objects, is this a bug?

    Thanks
    Andrew

  • Stu Smith

    #2
    Re: Variable scope bug?

    If the statement "new MyCOMObject.MyO bjectClass()" were to throw an
    exception then obj would not have been assigned, and thus the warning in the
    finally clause.

    You need to decide whether exceptions resulting from creating the object
    need to be handled here (in which case set obj to null beforehand), or
    whether you aren't handling them here (in which case create the object
    outside the try block).

    HTH,

    Stu


    "Andrew Todd" <andrew.todd@uk online.co.uk> wrote in message
    news:0bbc01c3a9 de$fe1552b0$a40 1280a@phx.gbl.. .[color=blue]
    > I've got a problem with the scope of a variable, consider
    > the following example:
    >
    >
    > MyCOMObject.MyO bjectClass obj;
    >
    > try
    > {
    > obj = new MyCOMObject.MyO bjectClass();
    > returnXml = obj.getEnquiryU sers();
    > }
    > catch (Exception e)
    > {
    > //handle error
    > }
    > finally
    > {
    > if (obj!=null)
    > {
    > Marshal.Release ComObject(obj);
    > obj = null;
    > }
    > }
    >
    > This won't compile, it tells me that the variable obj is
    > unassigned when I test if it's null in the finally
    > statement.
    >
    > This doesn't seem correct to me; I need to release the
    > COM object in the finally statement as if there is an
    > error in the try block the object will never be released.
    > These steps work in Java and are the steps we normally
    > take when interfacing with COM objects, is this a bug?
    >
    > Thanks
    > Andrew
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Variable scope bug?

      Andrew Todd <andrew.todd@uk online.co.uk> wrote:[color=blue]
      > I've got a problem with the scope of a variable, consider
      > the following example:[/color]

      <snip>
      [color=blue]
      > This won't compile, it tells me that the variable obj is
      > unassigned when I test if it's null in the finally
      > statement.[/color]

      Indeed, and it's right. Set it to null to start with. Local variables
      don't have any default value; they need to be definitely assigned
      before they're read.
      [color=blue]
      > This doesn't seem correct to me; I need to release the
      > COM object in the finally statement as if there is an
      > error in the try block the object will never be released.
      > These steps work in Java and are the steps we normally
      > take when interfacing with COM objects, is this a bug?[/color]

      No, it wouldn't work in Java. Here's an example showing it:

      public class Test
      {
      public static void main(String[] args)
      {
      String x;
      try
      {
      x = "hello";
      }
      catch (Exception e)
      {
      System.out.prin tln (e);
      }
      finally
      {
      if (x==null)
      {
      System.out.prin tln ("Oops");
      }
      }
      }
      }

      And compiling it:

      Test.java:16: variable x might not have been initialized
      if (x==null)
      ^
      1 error

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Andrew Todd

        #4
        Re: Variable scope bug?

        [color=blue]
        >Indeed, and it's right. Set it to null to start with.[/color]
        [color=blue]
        >No, it wouldn't work in Java. Here's an example showing[/color]
        it:

        Of course - you're right, I'd forgotten to set it to null
        initially!

        Thanks
        Andrew

        Comment

        Working...