hubFS: THE place for F#

. . . are you on The Hub?
Welcome to hubFS: THE place for F# Sign in | Join | Help
in Search

Disposable objects in classes

Last post 08-07-2008, 5:10 by Julien. 4 replies.
Sort Posts: Previous Next
  •  08-06-2008, 7:43 6532

    Disposable objects in classes

    Hi,

    When coding a class, is there a way to delete the disposable objects at the same time ? The "use" keywords not being allowed with implicit constructors.

    Thanks a lot.

    Julien

  •  08-06-2008, 12:24 6537 in reply to 6532

    Re: Disposable objects in classes

    Maybe you should implement IDisposable and override Finalize() ?
  •  08-06-2008, 14:30 6538 in reply to 6537

    Re: Disposable objects in classes

    Hi,

    perhaps Julian is referring to disposal of the member objects of a class instance?

    attempting to use implicit class constructors with use bindings causes the following error:

    type ParentClass() =

        use child = new ChildClass()

    Warning FS0191: 'use' bindings are not permitted in implicit class constructors. file1.fs

    using a let binding, however, doesn't call the Dispose function in child objects.

    #light

    module DisposalTest

    open System

    type ChildClass() =
        do printfn "creating object of class ChildClass"
        interface IDisposable with
            member x.Dispose() = printfn "disposed object of class ChildClass"

    type ParentClass() =
        let child = new ChildClass()
        do printfn "creating object of class ParentClass"
        interface IDisposable with
            member x.Dispose() = printfn "disposed object of class ParentClass"
           
    let main() =
        use parent = new ParentClass()
        ()
       
    do main()       

    produces the following output

    >project1.exe
    creating object of class ChildClass
    creating object of class ParentClass
    disposed object of class ParentClass

    notice that ChildClass.Dispose() is not called.

    warm regards,

    Danny

  •  08-06-2008, 22:26 6540 in reply to 6538

    Re: Disposable objects in classes

    You should override Dispose instead of reintroducing it's implementation. Or excplicitly call base's dispose.
  •  08-07-2008, 5:10 6542 in reply to 6540

    Re: Disposable objects in classes

    I was indeed wondering about the use of the "use" keyword in implicit constructions as mentioned by Danny Asher.

    The reason was that I saw the issue in the control.fs file of the distribution in the the mailbox class, and was wondering if there was a way to avoid implementing the IDisposable interface...

    Thanks for your help !

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems