Skip to content

Commit daa28a5

Browse files
committed
No longer carry reference to MsftDiscRecorder2.
Instantiate recorder as needed and release resource inside same method call instead of carrying the reference around and requiring a Dispose call on the object.
1 parent 8b5e3d3 commit daa28a5

7 files changed

+68
-53
lines changed

DiscRecorder.cs

+21-16
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
namespace IMAPI2
88
{
9-
public class DiscRecorder : IDisposable
9+
public class DiscRecorder
1010
{
11-
private MsftDiscRecorder2 _internal;
11+
private string _internalUniqueId;
1212
private ReadOnlyCollection<MediaProfile> _profiles;
1313

14-
internal MsftDiscRecorder2 Internal
14+
internal string InternalUniqueId
1515
{
16-
get { return _internal; }
16+
get { return _internalUniqueId; }
1717
}
1818
public ReadOnlyCollection<MediaProfile> SupportedProfiles
1919
{
@@ -22,16 +22,25 @@ public ReadOnlyCollection<MediaProfile> SupportedProfiles
2222

2323
internal DiscRecorder(string uniqueId)
2424
{
25-
List<MediaProfile> profiles;
26-
_internal = new MsftDiscRecorder2();
27-
_internal.InitializeDiscRecorder(uniqueId);
28-
29-
profiles = new List<MediaProfile>();
30-
foreach (IMAPI_PROFILE_TYPE item in _internal.SupportedProfiles)
25+
_internalUniqueId = uniqueId;
26+
MsftDiscRecorder2 recorder = null;
27+
try
28+
{
29+
List<MediaProfile> profiles;
30+
recorder = new MsftDiscRecorder2();
31+
recorder.InitializeDiscRecorder(uniqueId);
32+
33+
profiles = new List<MediaProfile>();
34+
foreach (IMAPI_PROFILE_TYPE item in recorder.SupportedProfiles)
35+
{
36+
profiles.Add((MediaProfile)item);
37+
}
38+
_profiles = profiles.AsReadOnly();
39+
}
40+
finally
3141
{
32-
profiles.Add((MediaProfile)item);
42+
if (recorder != null) Marshal.ReleaseComObject(recorder);
3343
}
34-
_profiles = profiles.AsReadOnly();
3544
}
3645

3746
private static string _GetProfileText(IMAPI_PROFILE_TYPE profileType)
@@ -103,9 +112,5 @@ private static string _GetProfileText(IMAPI_PROFILE_TYPE profileType)
103112
}
104113
}
105114

106-
void IDisposable.Dispose()
107-
{
108-
if (_internal != null) Marshal.ReleaseComObject(_internal);
109-
}
110115
}
111116
}

IMAPI2.csproj

+2-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
5050
</PropertyGroup>
5151
<PropertyGroup>
52-
<SignAssembly>true</SignAssembly>
52+
<SignAssembly>false</SignAssembly>
5353
</PropertyGroup>
5454
<PropertyGroup>
5555
<AssemblyOriginatorKeyFile>VibrantStrongName.snk</AssemblyOriginatorKeyFile>
@@ -66,6 +66,7 @@
6666
</ItemGroup>
6767
<ItemGroup>
6868
<Compile Include="BurnVerificationLevel.cs" />
69+
<Compile Include="RecorderNotFoundException.cs" />
6970
<Compile Include="FormatDataWriteAction.cs" />
7071
<Compile Include="FormatWriteUpdateEventArgs.cs" />
7172
<Compile Include="ImageUpdateEventArgs.cs" />
@@ -85,9 +86,6 @@
8586
<Compile Include="ReadOnlySelectableCollection.cs" />
8687
<Compile Include="MediaProfile.cs" />
8788
</ItemGroup>
88-
<ItemGroup>
89-
<None Include="VibrantStrongName.snk" />
90-
</ItemGroup>
9189
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
9290
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
9391
Other similar extension points exist, see Microsoft.Common.targets.

IMAPI2.suo

7.5 KB
Binary file not shown.

IMAPI2.v11.suo

-4.5 KB
Binary file not shown.

ImageMaster.cs

+29-32
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ namespace IMAPI2
1414
public class ImageMaster : IDisposable
1515
{
1616
private const string ClientName = "IMAPI2 Wrapper";
17-
17+
1818
private ReadOnlySelectableCollection<DiscRecorder> _recorders;
1919
private bool _isWriting;
2020
private long _mediaCapacity;
21-
private bool _mediaLoaded;
21+
private bool _mediaLoaded;
2222
private PhysicalMedia _media;
2323
private ReadOnlyCollection<MediaState> _mediaStates;
2424
private List<IMediaNode> _nodes;
@@ -60,11 +60,11 @@ public string VolumeLabel
6060
}
6161

6262
public ImageMaster()
63-
{
64-
List<DiscRecorder> recorders = new List<DiscRecorder>();
63+
{
64+
List<DiscRecorder> recorders = new List<DiscRecorder>();
6565
_media = PhysicalMedia.Unknown;
6666
_nodes = new List<IMediaNode>();
67-
_mediaStates = new ReadOnlyCollection<MediaState>(new List<MediaState> {MediaState.Unknown});
67+
_mediaStates = new ReadOnlyCollection<MediaState>(new List<MediaState> { MediaState.Unknown });
6868

6969
MsftDiscMaster2 discMaster = null;
7070
try
@@ -163,11 +163,13 @@ public void FormatMedia(bool quick, bool eject)
163163
if (!_mediaLoaded)
164164
throw new InvalidOperationException("LoadMedia must be called first.");
165165

166-
IDiscRecorder2 recorder = _recorders.SelectedItem.Internal;
166+
MsftDiscRecorder2 recorder = null;
167167
MsftDiscFormat2Erase discFormatErase = null;
168168

169169
try
170170
{
171+
recorder = new MsftDiscRecorder2();
172+
recorder.InitializeDiscRecorder(_recorders.SelectedItem.InternalUniqueId);
171173
discFormatErase = new MsftDiscFormat2Erase
172174
{
173175
Recorder = recorder,
@@ -183,6 +185,7 @@ public void FormatMedia(bool quick, bool eject)
183185
finally
184186
{
185187
if (discFormatErase != null) Marshal.ReleaseComObject(discFormatErase);
188+
if (recorder != null) Marshal.ReleaseComObject(recorder);
186189
}
187190
}
188191

@@ -196,15 +199,14 @@ public void LoadMedia()
196199
if (_recorders.SelectedIndex == -1)
197200
throw new InvalidOperationException("No DiscRecorder selected on the DiscRecorders list.");
198201

199-
MsftDiscRecorder2 recorder = _recorders.SelectedItem.Internal;
202+
MsftDiscRecorder2 recorder = null;
200203
MsftFileSystemImage image = null;
201204
MsftDiscFormat2Data format = null;
202205

203206
try
204207
{
205-
//
206-
// Create and initialize the IDiscFormat2Data
207-
//
208+
recorder = new MsftDiscRecorder2();
209+
recorder.InitializeDiscRecorder(_recorders.SelectedItem.InternalUniqueId);
208210
format = new MsftDiscFormat2Data();
209211
if (!format.IsCurrentMediaSupported(recorder))
210212
throw new MediaNotSupportedException("There is no media in the device.");
@@ -214,7 +216,7 @@ public void LoadMedia()
214216
//
215217
format.Recorder = recorder;
216218
_media = (PhysicalMedia)format.CurrentPhysicalMediaType;
217-
219+
218220
mediaStateFlags = (long)format.CurrentMediaStatus;
219221
foreach (MediaState state in Enum.GetValues(typeof(MediaState)))
220222
{
@@ -229,7 +231,7 @@ public void LoadMedia()
229231
//
230232
// Create a file system and select the media type
231233
//
232-
image = new MsftFileSystemImage();
234+
image = new MsftFileSystemImage();
233235
image.ChooseImageDefaultsForMediaType((IMAPI_MEDIA_PHYSICAL_TYPE)_media);
234236

235237
//
@@ -246,8 +248,9 @@ public void LoadMedia()
246248
}
247249
finally
248250
{
249-
if (format != null) Marshal.ReleaseComObject(format);
250251
if (image != null) Marshal.ReleaseComObject(image);
252+
if (format != null) Marshal.ReleaseComObject(format);
253+
if (recorder != null) Marshal.ReleaseComObject(recorder);
251254
}
252255
}
253256

@@ -256,14 +259,13 @@ public void LoadRecorder()
256259
if (_recorders.SelectedIndex == -1)
257260
throw new InvalidOperationException("No DiscRecorder selected from the DiscRecorders list.");
258261

259-
var recorder = _recorders.SelectedItem.Internal;
260-
261-
//
262-
// Verify recorder is supported
263-
//
262+
MsftDiscRecorder2 recorder = null;
264263
IDiscFormat2Data discFormatData = null;
265264
try
266265
{
266+
recorder = new MsftDiscRecorder2();
267+
recorder.InitializeDiscRecorder(_recorders.SelectedItem.InternalUniqueId);
268+
267269
discFormatData = new MsftDiscFormat2Data();
268270
switch (discFormatData.IsRecorderSupported(recorder))
269271
{
@@ -277,10 +279,8 @@ public void LoadRecorder()
277279
}
278280
finally
279281
{
280-
if (discFormatData != null)
281-
{
282-
Marshal.ReleaseComObject(discFormatData);
283-
}
282+
if (discFormatData != null) Marshal.ReleaseComObject(discFormatData);
283+
if (recorder != null) Marshal.ReleaseComObject(recorder);
284284
}
285285
}
286286

@@ -294,7 +294,8 @@ public void WriteImage(BurnVerificationLevel verification, bool finalize, bool e
294294

295295
try
296296
{
297-
recorder = _recorders.SelectedItem.Internal;
297+
recorder = new MsftDiscRecorder2();
298+
recorder.InitializeDiscRecorder(_recorders.SelectedItem.InternalUniqueId);
298299

299300
discFormatData = new MsftDiscFormat2Data
300301
{
@@ -333,22 +334,18 @@ public void WriteImage(BurnVerificationLevel verification, bool finalize, bool e
333334
}
334335
finally
335336
{
336-
if (fileSystem != null)
337-
{
338-
Marshal.FinalReleaseComObject(fileSystem);
339-
}
337+
if (fileSystem != null) Marshal.FinalReleaseComObject(fileSystem);
340338
}
341339

342340
discFormatData.Update -= _discFormatWrite_Update;
343341

344-
if (eject)
345-
recorder.EjectMedia();
342+
if (eject) recorder.EjectMedia();
346343
}
347344
finally
348345
{
349-
if (recorder != null) Marshal.ReleaseComObject(recorder);
350-
if (discFormatData != null) Marshal.ReleaseComObject(discFormatData);
351346
_isWriting = false;
347+
if (discFormatData != null) Marshal.ReleaseComObject(discFormatData);
348+
if (recorder != null) Marshal.ReleaseComObject(recorder);
352349
}
353350
}
354351

@@ -371,7 +368,7 @@ void IDisposable.Dispose()
371368
if ((_writerThread != null) && (_writerThread.IsAlive)) _writerThread.Join();
372369
foreach (DiscRecorder recorder in _recorders)
373370
{
374-
Marshal.ReleaseComObject(recorder.Internal);
371+
Marshal.ReleaseComObject(recorder.InternalUniqueId);
375372
}
376373
}
377374
}

RecorderNotFoundException.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace IMAPI2
7+
{
8+
public class RecorderNotFoundException : RecorderNotSupportedException
9+
{
10+
public RecorderNotFoundException(string message)
11+
: base(message)
12+
{
13+
}
14+
}
15+
}

RecorderNotSupportedException.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace IMAPI2
77
{
8-
class RecorderNotSupportedException : NotSupportedException
8+
public class RecorderNotSupportedException : NotSupportedException
99
{
1010
public RecorderNotSupportedException(string message)
1111
: base(message)

0 commit comments

Comments
 (0)