Skip to content

Commit

Permalink
Now properly disposing the matlab com object. Multi-thread access pro…
Browse files Browse the repository at this point in the history
…blems should no longer occur.

Added AssemblyVisibleTo in the hopes that I can clean up the API
  • Loading branch information
Rickasaurus committed Oct 19, 2013
1 parent 401de60 commit dbee202
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ open FSMatlab.InterfaceTypes

open TestHelpers


[<Fact>]
let ``function calls with arrays should execute and return a single correct answer`` () =
let res = Toolboxes.matlab.elfun.nthroot([|9.0; 49.0|], 2.0) |> EGT1<double []>
Expand Down
11 changes: 11 additions & 0 deletions MatlabTypeProvider/MatlabTypeProvider/AssemblyInfo.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module private AssemblyInfo

open System.Reflection
open System.Runtime.CompilerServices
open System.Runtime.InteropServices

[<assembly: InternalsVisibleTo("MatlabTypeProvider.Tests")>]
do()



26 changes: 26 additions & 0 deletions MatlabTypeProvider/MatlabTypeProvider/MatlabManualCOM.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module MatlabCOM =

let comType = Type.GetTypeFromProgID(progid, true)

//Todo: Marshal ReleaseCOMObject in Dispose

[<ThreadStatic>] [<DefaultValue>]
static val mutable private instance:Option<Object>

Expand All @@ -39,6 +41,30 @@ module MatlabCOM =
member t.MatlabInstance = getComObject ()
#endif

//
// IDisposable for Com Object
//

let mutable _disposed = false
let dispose (disposing: bool) =
if not _disposed && disposing then
() // IDisposable only

if not _disposed then
// Unmanaged Stuff
MatlabCOMProxy.instance |> function | Some(v) -> Marshal.ReleaseComObject(v) |> ignore | None -> ()
_disposed <- true


interface IDisposable with
member t.Dispose () = dispose(true); GC.SuppressFinalize(t)

override t.Finalize () = dispose(false)

//
// Members
//

/// The server returns output from the command in the string, result. The result string also contains any warning or error messages that might have been issued by MATLAB software as a result of the command.
member t.Execute (args: obj[]) =
lock (lockObj) (fun _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets" Condition=" Exists('$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets')" />
<ItemGroup>
<Compile Include="AssemblyInfo.fs" />
<Compile Include="ProvidedTypes.fs" />
<Compile Include="MatlabDLL.fs" />
<Compile Include="MatlabManualCOM.fs" />
Expand Down

0 comments on commit dbee202

Please sign in to comment.