What is/is there a proper way to force COM references immediately released?

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

What is/is there a proper way to force COM references immediately released?
 
Shamil




Posts: 7
Joined: 2010-02-06
Hi All --

Could you please advise how to change the folowing code (of ADX for .NET MS Access COM Add-In) to have COM references immediately released on exiting fr om every method?

Please feel free to give just a link to topics/threads here or somewhere on Internet wh ere the subject issue solution is presented.

Thank you.

--
Shamil

P.S. Code sample
...
public void InitDat abase()
{
dao.Database dbs = null;

try
{
dbs = _application.CurrentDb();

if (dbs == null)
throw new ApplicationException(
string.Format("CurrentDatabase is not available, dbs = null")
);

... do some custom actions with dbs here ...
}
catch { }
finally
{
dbs = null;
_database = new AccessDat abase(this);
}

}

public string CurrentDatabaseFullPath
{
get
{
dao.Database dbs = _application.CurrentDb();
string fullPath = dbs.Name;
dbs = null;
return fullPath;
}
}

Note: _application is a reference to COM Add-In's MS Access application inatance set on add-in's initialization phase - no need to release it.

private acc.Application _application;
public void SetHostApplication(object app)
{
_application = app as acc.Application;
}
...
Posted 27 Apr, 2010 02:25:56 Top
Andrei Smolin


Add-in Express team


Posts: 17123
Joined: 2006-05-11
Hi Shamil,

        public void InitDatabase()
        {
            dao.Database dbs = null;
            try
            {
                dbs = _application.CurrentDb();

                if (dbs == null)
                    throw new ApplicationException(
                        string.Format("CurrentDatabase is not available, dbs = null")
                        );

                ... do some custom actions with dbs here ...
            }
            catch { }
            finally
            {
                if (dbs != null) Marshal.ReleaseComObject(dbs);
                dbs = null;
                _database = new AccessDatabase(this);
            }
        }

        public string CurrentDatabaseFullPath
        {
            get
            {
                dao.Database dbs = _application.CurrentDb();
                string fullPath = dbs.Name;
                if (dbs != null) Marshal.ReleaseComObject(dbs);
                dbs = null;
                return fullPath;
            }
        }

        private acc.Application _application;
        public void SetHostApplication(object app)
        {
            _application = app as acc.Application;
            // no need to release anything
        }


Please see Why to release COM objects in Office add-ins?

Regards from Belarus (GMT+2),

Andrei Smolin
Add-in Express Team Leader
Posted 27 Apr, 2010 03:06:45 Top
Shamil




Posts: 7
Joined: 2010-02-06
Thank you, Andrei.
Posted 27 Apr, 2010 03:21:30 Top