# Do you have to dispose of the SPWeb in the EventReceiver when using properties.OpenWeb()?

Does properties.OpenWeb() create a new SPRequest object or does it refer to the existing SPWeb object already there in the properties object? Should we manually dispose of the SPWeb obtained in this manner?

Is the second line safer?

        SPWeb _web = properties.OpenWeb();
SPWeb _web = properties.ListItem.ParentList.ParentWeb;

• By the way, current SPDisposeCheck fails to detect this undisposed SPWeb scenario. – Alexey Krasheninnikov Jan 25 '10 at 9:23

In SharePoint 2007 you need to dispose the returned SPWeb if you use properties.OpenWeb()

In SP2010 you should use the SPItemEventProperties.Web property instead. This performs better than OpenWeb and has no dispose issues.

Should nobody prove me wrong, I shall presume (until I resort to ILDASM / Reflector) that the safe way in ItemEventReceivers would be to use

SPWeb _web = properties.ListItem.ParentList.ParentWeb;


Surely,

if (properties.ListItem != null) SPWeb _web = properties.ListItem.ParentList.ParentWeb;


or

bool _shouldDispose = false;
try {_web = properties.ListItem.ParentList.ParentWeb;} catch (SPException ex) {_web = properties.OpenWeb(); _shouldDispose=true;}

• fyi, depending on the type of item event, properties.ListItem could be null (i.e. ItemDeleted) – Jaap Vossers Feb 2 '10 at 19:31