1. you should initiate a SPSite object with in the SPSecurity.RunWithElevetedPrivilges.
2. Create SPSite using ID. This is very important thing.
3. Make sure to add web.AllowUnsafeUpdates = true before your logic and add web.AllowUnsafeUpdates = false after your logic
See the below sample -
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWeb webContxt = SPContext.Current.Web;
Guid newSite;
using (SPSite site = new SPSite(webContxt.Site.ID))
{
using (SPWeb web = site.OpenWeb(webContxt.ID))
{
web.AllowUnsafeUpdates = true;
//Add your code
web.AllowUnsafeUpdates = false;
}
}
});