c# - SharePoint memory leak -


does contain memory leak? i've been trying better understand memory leaks, can't tell if have corrected this? if not, how correctly dispose of spweb object , spsite object?

using (spweb owebsite = new spsite(weburl).openweb()) //open sp web {     splistcollection colllist = owebsite.lists; //open lists      foreach (splist olist in colllist)     //for each list execute     {         if (!olist.hidden)         //if list hidden else nothing         {             listsitesdropdownbox.items.add(new listitem(spencode.htmlencode(olist.title), spencode.htmlencode(olist.title)));             viewstate["item" + counter] = spencode.htmlencode(olist.title);             counter++;         }     } } 

yep, does. dispose of spweb forget disposing of spsite.
right way this:

using (var site = new spsite(weburl)) using (var web = site.openweb()) {     // ... } 

note equivalent to:

using (var site = new spsite(weburl)) {     using (var web = site.openweb()) {         // ...     } } 

but omitted braces outer using reduce code nesting. bracing rules same if.

a few stylistic remarks:

  • please, don't use systems hungarian in c# code. list.
  • comments rephrase code above don't make sense. keep them constructive, i.e. explaining purpose of code. instead of 4 comments, should've written in beginning:

    // populate drop-down list list names , save them in viewstate 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -