Showing posts with label Visual Studio 2008. Show all posts
Showing posts with label Visual Studio 2008. Show all posts

Thursday, January 22, 2009

Access Violations, Interop and VS2008 SP1

After installing SP1 for VS2008 the other day, one of our programs stopped working. In fact, it came up with an AccessViolationException:

System.AccessViolationException: Attempted to read or write protected
memory. This is often an indication that other memory is corrupt.
at VariantClear(tagVARIANT* pvarg)
at
System.Runtime.InteropServices.CustomMarshalers.EnumeratorViewOfEnumVariant.MoveNext()
at ...etc...

This was a surprise, because it ran fine before. Or, at least, that is what we thought.

An examination of the code revealed the exception was being thrown in a foreach() statement. Odd, but the foreach() statement was looping on a COM Interop collection. I've done a little Outlook Add-In development and one thing I've learnt about COM Interop is that it doesn't handle memory the way you would think .NET handles it.

Basically, there are two thing you need to do:
1) Do NOT use foreach() - these are bad, use for(;;)
2) Be very careful about releasing memory for references to COM objects behind Interop interfaces

There is a great article regarding this here:

OOM.NET: Part 2 - Outlook Item Leaks

If you do any Interop work, you really should read and understand it.

Back to the code that failed:

     private static bool ColIsForeignKey(Table t, Column col)
{
foreach (Key key in t.Keys)
{
if (key.Type != SQLDMO_KEY_TYPE.SQLDMOKey_Foreign) continue;
foreach (string keyCol in key.KeyColumns)
{
if (keyCol.ToLower() == col.Name.ToLower())
{
return true;
}
}
}
return false;
}

Now here is the code modified:
     private static bool ColIsForeignKey(Table t, Column col)
{
SQLDMO.Keys keys = t.Keys;
try
{
for (int i = 1; i <= keys.Count; i++)
{
Key key = keys.Item(i);
try
{
if (key.Type != SQLDMO_KEY_TYPE.SQLDMOKey_Foreign) continue;

Names columns = key.KeyColumns;
try
{
for (int j = 1; j <= columns.Count; j++)
{
string keyCol = columns.Item(j);
if (keyCol.ToLower() == col.Name.ToLower())
{
return true;
}
}
}
finally
{
Marshal.ReleaseComObject(columns);
columns = null;
}
}
finally
{
Marshal.ReleaseComObject(key);
key = null;
}
}
}
finally
{
Marshal.ReleaseComObject(keys);
keys = null;
}
return false;
}

It looks a lot uglier, but you can be sure you're not keeping any nasty
references around the place.

Friday, September 05, 2008

Visual Studio Using Huge Amounts of Memory

Lately I have been working on a solution with 33 projects on Visual Studio 2008.

Yeah, I know. That's a lot.

A lot of them were test projects and it is SCSF-based, so that is okay, but Visual Studio was chewing up loads of memory. I mean serious amounts, towards the 2GB limit. At this point, my hard disk would thrash at the smallest use of the IDE and be unusable for minutes on end.

Someone put me on to a tool from JetBrains, the guys that did ReSharper - it is supposed to change the way memory is allocated. One of the guys on our project who is using this tool is now using a lot less memory, but whilst mine is better than it was, it still aint that great.

So it may help.

Another option is to add /3GB /USERVA=3030 to the boot.ini file. Visual Studio is already large address aware (I think), so it should allow more VM use for the devenv.exe process.


Update:
One of the biggest causes of this was DevExpress Refactor! Pro. I'm normally a big fan of this add-in, but uninstalling it saved me enormous amounts of memory. 

Thursday, July 31, 2008

Including Setup Projects in Team Build

Setup (Deployment) projects are not supported by Team Build out of the box. You have to add
additional steps to the TFSBuild.proj file to build the setup project and copy the files to the output directory.

The following MSDN article describes this:

Walkthrough: Configuring Team Build to Build a Visual Studio Setup Project

Monday, March 31, 2008

PowerCommands for Visual Studio 2008

This extremely handy set of extensions for Visual Studio 2008 provides extra functionality to various parts of the IDE.

PowerCommands for Visual Studio 2008

Personally, I think these are the sort of things that should have been provided out of the box.

Friday, January 11, 2008

Listening to Calendar Events with Outlook 2007 and VSTO

I've recently being trying to listen to events from a user's calendars so that I can track when appointments are added, removed and modified, and ran into a couple of problems.

Problem 1: Events intermittently stop firing

I was listening to events on the MAPIFolder object and the Calendar's Items collection and they would work for a while and at some point would stop firing.

I eventually traced this down to what I believe is a COM reference counting issue. I wasn't storing a reference to these objects, so I guess there was no implicit AddRef by the interop library. As such, there was no need for the runtime to keep the object around.

Hanging on to these references appears to be resolving this issue, so I stand by my assumptions.

Problem 2: How do we know what has been deleted?

The events I was hooking up to were on the ItemsEvents_Event interface off the MAPIFolder.Items property. There are thee events on this interface:

  • ItemAdd

  • ItemChange

  • ItemRemove


The first two have a single parameter which will be the AppointmentItem that is either being added or changed. Great! The last one doesn't have any parameters - all it tells you is that an item has been removed, but doesn't give you any indication what has been deleted. Besides that, its actually too late anyway, the item has already been deleted (you cannot cancel it).

So, I am now hooking up to the BeforeItemMove event on the MAPIFolderEvents_12_Event interface off the MAPIFolder object. This gives me a reference to the AppointmentItem being moved, the folder in which it is being moved to, and also allows me to cancel it.

To detect that it is being deleted, just check the folder it is being moved to is null (hard-delete such as Shift-Del) or the deleted items folder (soft-delete).

Solution

I have created a class that will monitor a user's default calendar and fires three events:

  • AppointmentAdded - gives you the AppointmentItem instance added.

  • AppointmentModified - gives you the AppointmentItem instance changed.

  • AppointmentDeleting - gives you the AppointmentItem that is about to be deleted and allows you to cancel the operation if needed.


Sample usage:

The following code is for use within the ThisAddIn class. It outputs to console when an item is added, modified, or about to be deleted. It also displays a dialog box confirming an item should be deleted.
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
CalendarMonitor monitor = new CalendarMonitor(this.Application.ActiveExplorer());
monitor.AppointmentAdded +=
new EventHandler<EventArgs<AppointmentItem>>(monitor_AppointmentAdded);
monitor.AppointmentModified +=
new EventHandler<EventArgs<AppointmentItem>>(monitor_AppointmentModified);
monitor.AppointmentDeleting +=
new EventHandler<CancelEventArgs<AppointmentItem>>(monitor_AppointmentDeleting);
}

private void monitor_AppointmentAdded(object sender, EventArgs<AppointmentItem> e)
{
Debug.Print("Appointment Added: {0}", e.Value.GlobalAppointmentID);
}

private void monitor_AppointmentModified(object sender, EventArgs<AppointmentItem> e)
{
Debug.Print("Appointment Modified: {0}", e.Value.GlobalAppointmentID);
}

private void monitor_AppointmentDeleting(object sender, CancelEventArgs<AppointmentItem> e)
{
Debug.Print("Appointment Deleting: {0}", e.Value.GlobalAppointmentID);
DialogResult dr = MessageBox.Show("Delete appointment?", "Confirm",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.No)
{
e.Cancel = true;
}
}

There are two other classes used: EventArgs and CancelEventArgs. These generics are used by the events to pass strongly typed appointments.

EventArgs.cs:
using System;
using System.Collections.Generic;

namespace OutlookAddIn2
{
public class EventArgs<T> : EventArgs
{
private T _value;

public EventArgs(T aValue)
{
_value = aValue;
}

public T Value
{
get { return _value; }
set { _value = value; }
}
}
}

CancelEventArgs.cs:
using System;
using System.Collections.Generic;

namespace OutlookAddIn2
{
public class CancelEventArgs<T> : EventArgs<T>
{
private bool _cancel;

public CancelEventArgs(T aValue)
: base(aValue)
{
}

public bool Cancel
{
get { return _cancel; }
set { _cancel = value; }
}
}
}

CalendarMonitor.cs:
using System;
using System.Collections.Generic;
using Microsoft.Office.Interop.Outlook;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace OutlookAddIn2
{
public class CalendarMonitor
{
private Explorer _explorer;
private List<string> _folderPaths;
private List<MAPIFolder> _calendarFolders;
private List<Items> _calendarItems;
private MAPIFolder _deletedItemsFolder;

public event EventHandler<EventArgs<AppointmentItem>> AppointmentAdded;
public event EventHandler<EventArgs<AppointmentItem>> AppointmentModified;
public event EventHandler<CancelEventArgs<AppointmentItem>> AppointmentDeleting;

public CalendarMonitor(Explorer anExplorer)
{
_folderPaths = new List<string>();
_calendarFolders = new List<MAPIFolder>();
_calendarItems = new List<Items>();

_explorer = anExplorer;
_explorer.BeforeFolderSwitch +=
new ExplorerEvents_10_BeforeFolderSwitchEventHandler(Explorer_BeforeFolderSwitch);

NameSpace session = _explorer.Session;
try
{
_deletedItemsFolder = session.GetDefaultFolder(OlDefaultFolders.olFolderDeletedItems);
HookupDefaultCalendarEvents(session);
}
finally
{
Marshal.ReleaseComObject(session);
session = null;
}
}

private void HookupDefaultCalendarEvents(NameSpace aSession)
{
MAPIFolder folder = aSession.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
if (folder != null)
{
try
{
HookupCalendarEvents(folder);
}
finally
{
Marshal.ReleaseComObject(folder);
folder = null;
}
}
}

private void Explorer_BeforeFolderSwitch(object aNewFolder, ref bool Cancel)
{
MAPIFolder folder = (aNewFolder as MAPIFolder);
//
// Hookup events to any other Calendar folder opened.
//
if (folder != null)
{
try
{
if (folder.DefaultItemType == OlItemType.olAppointmentItem)
{
HookupCalendarEvents(folder);
}
}
finally
{
Marshal.ReleaseComObject(folder);
folder = null;
}
}
}

private void HookupCalendarEvents(MAPIFolder aCalendarFolder)
{
if (aCalendarFolder.DefaultItemType != OlItemType.olAppointmentItem)
{
throw new ArgumentException("The MAPIFolder must use " +
"AppointmentItems as the default type.");
}
//
// Ignore other user's calendars.
//
if ((_folderPaths.Contains(aCalendarFolder.FolderPath) == false)
&& (IsUsersCalendar(aCalendarFolder)))
{
Items items = aCalendarFolder.Items;
//
// Store folder path to prevent double ups on our listeners.
//
_folderPaths.Add(aCalendarFolder.FolderPath);
//
// Store a reference to the folder and to the items collection so that it remains alive for
// as long as we want. This keeps the ref count up on the underlying COM object and prevents
// it from being intermittently released (then the events don't get fired).
//
_calendarFolders.Add(aCalendarFolder);
_calendarItems.Add(items);
//
// Add listeners for the events we need.
//
((MAPIFolderEvents_12_Event)aCalendarFolder).BeforeItemMove +=
new MAPIFolderEvents_12_BeforeItemMoveEventHandler(Calendar_BeforeItemMove);
items.ItemChange += new ItemsEvents_ItemChangeEventHandler(CalendarItems_ItemChange);
items.ItemAdd += new ItemsEvents_ItemAddEventHandler(CalendarItems_ItemAdd);
}
}

private void CalendarItems_ItemAdd(object anItem)
{
AppointmentItem appointment = (anItem as AppointmentItem);
if (appointment != null)
{
try
{
if (this.AppointmentAdded != null)
{
this.AppointmentAdded(this, new EventArgs<AppointmentItem>(appointment));
}
}
finally
{
Marshal.ReleaseComObject(appointment);
appointment = null;
}
}
}

private void CalendarItems_ItemChange(object anItem)
{
AppointmentItem appointment = (anItem as AppointmentItem);
if (appointment != null)
{
try
{
if (this.AppointmentModified != null)
{
this.AppointmentModified(this, new EventArgs<AppointmentItem>(appointment));
}
}
finally
{
Marshal.ReleaseComObject(appointment);
appointment = null;
}
}
}

private void Calendar_BeforeItemMove(object anItem, MAPIFolder aMoveToFolder, ref bool Cancel)
{
if ((aMoveToFolder == null) || (IsDeletedItemsFolder(aMoveToFolder)))
{
AppointmentItem appointment = (anItem as AppointmentItem);
if (appointment != null)
{
try
{
if (this.AppointmentDeleting != null)
{
//
// Listeners to the AppointmentDeleting event can cancel the move operation if moving
// to the deleted items folder.
//
CancelEventArgs<AppointmentItem> args = new CancelEventArgs<AppointmentItem>(appointment);
this.AppointmentDeleting(this, args);
Cancel = args.Cancel;
}
}
finally
{
Marshal.ReleaseComObject(appointment);
appointment = null;
}
}
}
}

private bool IsUsersCalendar(MAPIFolder aFolder)
{
//
// This is based purely on my observations so far - a better way?
//
return (aFolder.Store != null);
}

private bool IsDeletedItemsFolder(MAPIFolder aFolder)
{
return (aFolder.EntryID == _deletedItemsFolder.EntryID);
}
}
}

Wednesday, January 09, 2008

Maintaining Custom Toolbar Positions in Outlook 2007

NOTE: This applies to Visual Studio 2008 and Outlook 2007.

There are a few articles floating around have examples on saving toolbar position information at the end of a session and restoring that position the next time Outlook is loaded. One at MSDN applies to Visual Studio 2008 and .NET Framework 3.5:

How to: Maintain Position Information for Custom Toolbars between Outlook Sessions

I tried this code with Outlook 2007 and it throws an exception on shutdown when it tries to save the toolbar position information. Reading the article again, I noticed that it says it applies to Outlook 2003.

I just assumed that it would work with Outlook 2007, and why would they release an article for Visual Studio 2008 that works with an old version of Outlook?

The exception that is being thrown when the CommandBar instance is accessed is:

System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x800A01A8

Look up this HRESULT value and you can see that it means "Object Required".

To me, this means that the CommandBar has already been cleaned up and is no longer accessible. ThisAddIn_Shutdown is supposedly the first user code to be run before a shutdown occurs, so what do you do?

I have settled on listening to the Close event of the main Explorer window (the one the CommandBar is added to). This is called before ThisAddIn_Shutdown and the CommandBar is still accessible.

Example:

  ...

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Outlook.Explorer explorer = this.Application.ActiveExplorer();

if (explorer != null)
{
((ExplorerEvents_10_Event)explorer).Close += new ExplorerEvents_10_CloseEventHandler(ThisAddIn_Close);

commandBar = explorer.CommandBars.Add(TOOLBARNAME,
Office.MsoBarPosition.msoBarFloating, false, true);

...
}
}

private void ThisAddIn_Close()
{
SaveCommandBarSettings();
}

...

Powered By Blogger