[Question] ReleaseCOMObjects - When to use?

Add-in Express™ Support Service
That's what is more important than anything else

[Question] ReleaseCOMObjects - When to use?
 
Sven Heitmann




Posts: 193
Joined: 2004-08-23
Hi,

I have a question about the Marshal.ReleaseComObject:

This is some code I'am using, can you give me some hints if the using of Marshal.ReleaseComObject is ok this way?

private void CreateStandardStyle() {
         object oType = Word.WdStyleType.wdStyleTypeParagraph;
         object nextParagraph = StandardStyle;
         object baseStyle = "";

         Word._Application oWord = WordHost();
         Word.Document oDocument = oWord.ActiveDocument;
         Word.Styles oStyles = oDocument.Styles;
         Word.Style oStyle = oStyles.Add(StandardStyle, ref oType);
         Word.Font oFont = oStyle.Font;
         Word.ParagraphFormat oFormat = oStyle.ParagraphFormat;
         Word.Borders oBorders = oFormat.Borders; 
         
         //----- Font
         oFont.Name = "Times New Roman";
         oFont.Size = 12.0f;
         //----- Paragraph
         oFormat.TabStops.ClearAll();
         oFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpace1pt5;
         oFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify;
         oFormat.WidowControl = 0;
         oFormat.Hyphenation = -1;
         oFormat.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevelBodyText;
         //----- Borders
         oBorders.DistanceFromTop = 1;
         oBorders.DistanceFromLeft = 4;
         oBorders.DistanceFromBottom = 1;
         oBorders.DistanceFromRight = 4;
         //----- Misc
         oStyle.set_NextParagraphStyle(ref nextParagraph);
         oStyle.set_BaseStyle(ref baseStyle);
         oStyle.LanguageID = Word.WdLanguageID.wdGerman;
         oStyle.Frame.Delete();

         Marshal.ReleaseComObject(oBorders);
         Marshal.ReleaseComObject(oFormat);
         Marshal.ReleaseComObject(oFont);
         Marshal.ReleaseComObject(oStyle);
         Marshal.ReleaseComObject(oStyles);
         Marshal.ReleaseComObject(oDocument);
         Marshal.ReleaseComObject(oWord);
      }


Best regards,
Sven
Best regards,

Sven Heitmann
Posted 27 Sep, 2004 10:36:56 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi,

you do everything correctly. You can also use the following code to make sure that all references to a COM object were released:
while (Marshal.ReleaseComObject(yourObject) > 0);
Posted 27 Sep, 2004 10:55:05 Top
Sven Heitmann




Posts: 193
Joined: 2004-08-23
Ok, thank you for your tip =)

Now I have to recheck my code :o
Best regards,

Sven Heitmann
Posted 28 Sep, 2004 02:02:29 Top
Sven Heitmann




Posts: 193
Joined: 2004-08-23
edit:

Ok... I got some trouble when releasing the Word._Application Object... when I release it and wan't to use HostApllication a second time an Exception is thrown because the Word._Application Object was disconnected from the RCW...

so I think releasing all ComObjects apart from the Word._Application Object is a good behavior...
Best regards,

Sven Heitmann
Posted 29 Sep, 2004 04:34:05 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi,

you shouldn't release the Word._Application Object. This will be done by ADX.

so I think releasing all ComObjects apart from the Word._Application Object is a good behavior...


Yes, it will be the right way.
Posted 30 Sep, 2004 08:48:01 Top