Search code examples
vb.netvisual-studio-2012return-valuestatic-analysisca2000

CA2000 warning on returned value


I know this has been discussed many times but no solutions seems to work so I thought and may be worth a try to reopen it since some time has passed.

I have a function:

Public Function Test() As Object
    Dim retVal As DisposableObject
    Dim tempRetVal As DisposableObject
    Try
        tempRetVal = New DisposableObject
        retVal = tempRetVal
        tempRetVal = Nothing
        Return retVal
    Catch
        tempRetVal.Dispose()
        Throw
    Finally
        tempRetVal.Dispose()
    End Try
End Function

As you can see, there are a lot of Dispose statements. This is because I was trying to find a way to have it working. The only way I found (which clearly is not a solution) was to add retVal.Dispose() before returning retval.

 Public Function Test() As Object
    Dim retVal As DisposableObject
    Dim tempRetVal As DisposableObject
    Try
        tempRetVal = New DisposableObject
        retVal = tempRetVal
        tempRetVal = Nothing
        retVal.Dispose()
        Return retVal
    Catch
        tempRetVal.Dispose()
        Throw
    Finally
        tempRetVal.Dispose()
    End Try
End Function

Any hint will be gladly appreciated! :)

Note: I'm using VS2012

EDIT: I also tried the simple template proposed by MS and it doesn't work either:

Public Function Test() As Object
    Dim retVal As DisposableObject
    Dim tempRetVal As DisposableObject
    Try
        tempRetVal = New DisposableObject
        retVal = tempRetVal
        tempRetVal = Nothing
        Return retVal
    Finally
        if tempRetVal isnot Nothing then tempRetVal.Dispose()
    End Try
End Function

CA2000 is thrown on tempRetVal = New DisposableObject.


Solution

  • The only way I found (which clearly is not a solution) was to add retVal.Dispose() before returning retval.

    Why? You anyways have a finally block defined. Let the finally block take care of the disposal. Your code should look like below. Also,

     Public Function Test() As Object
        Dim retVal As LLServerConnection
        Dim tempRetVal As LLServerConnection
        Try
            tempRetVal = New LLServerConnection
            retVal = tempRetVal
            tempRetVal = Nothing
            Return retVal
        Catch
            Throw
        Finally
            If Not tempRetVal Is Nothing Then
            tempRetVal.Dispose()
        End Try
    End Function
    

    See CA2000: Dispose objects before losing scope for more information.

    EDIT:

    To my believe, you are getting that CA2000 warning message because of the return statement inside TRY block. Rather, return the obejct before your subroutine actually end. See below code with comment added to the changes. This will be fine now.

    Public Function Test() As DisposableObject //Change object to actual type DisposableObject
        Dim retVal As DisposableObject = Nothing
        Dim tempRetVal As DisposableObject = Nothing
    
        Try
            tempRetVal = New DisposableObject()
            retVal = tempRetVal
            tempRetVal = Nothing
    
        Finally
          If Not tempRetVal Is Nothing Then
             tempRetVal.Dispose()
          End If 
    
        End Try
    
            Return retVal //Return the object before your subroutine ends
    
    End Function