Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 62345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:27:41+00:00 2026-05-10T18:27:41+00:00

I’m stuck with .Net 1.1 application (i.e. I can not use the generics goodies

  • 0

I’m stuck with .Net 1.1 application (i.e. I can not use the generics goodies from 2.0 for now), and I was trying to optimize some parts of the code. As it deals a lot with runtime callable wrappers, which need to be released, I ended up to create a utility method which loops until all references are released. The signature of the method is:

void ReleaseObject(object comObject) 

After releasing all comObjects, I call GC.Collect and GC.WaitForPendingFinalizers (don’t ask – anybody dealing with Office interop knows).

And … as usual, I hit a corner case – if I do not assign the corresponding managed reference to null before the GC.Collect call, it does not cleanup properly.

So, my code looks like:

ReleaseObject(myComObject); myComObject = null; GC.Collect() ... 

As, there are a bunch of xxx=null, I decided to put this in the util method, but as there is a difference between passing by reference, and passing a reference parameter, obviously I had to change the method to:

void ReleaseObject(out object comObject) {    //do release    comObject = null; } 

and edit the caller to:

MyComClass myComObject = xxxx; ReleaseObject(out myComObject); 

This fails with a message: ‘Cannot convert from ‘out MyComClass’ to ‘out object”

While I can think of why it can be a problem (i.e. the reverse cast from object to MyComClass is not implicit, and there is no guarantee what the method will do), I was wondering if there is a workaround, or I need to stay with my hundreds assignments of nulls.

Note: I have a bunch of different COM objects types, thats why I need a ‘object’ parameter, and not a type safe one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:27:41+00:00Added an answer on May 10, 2026 at 6:27 pm

    Why is it better to call a method than to just set the variable to null? They’re both single line calls, and the latter is a lot simpler.

    It does sound very odd that you need to set them to null in the first place though. Are these static variables, or instance variables whose values need to be released earlier than their containing object? If the variable is just a local variable which will go out of scope anyway, setting it to null shouldn’t make any difference (in release).

    Do the RCWs not implement IDisposable? If so, calling Dispose (preferably via a using statement) would be the best bet.

    (After discussions in comments.)

    These are local variables, which aren’t referenced later in the method. That means the garbage collector will realise that they don’t need to be treated as ‘root’ references – so setting them to null shouldn’t make any difference.

    To answer the original question directly, however: no, you can’t pass a variable by reference unless the method parameter is of exactly the same type, so you’re out of luck here. (With generics it would be possible, but you’ve said you’re limited to .NET 1.1.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.