Attempted to read or write protected memory

Posted by Interfector on Stack Overflow See other posts from Stack Overflow or by Interfector
Published on 2011-01-01T23:09:48Z Indexed on 2011/01/02 8:53 UTC
Read the original article Hit count: 363

I have a sample ASP.NET MVC 3 web application that is following Jonathan McCracken's Test-Drive Asp.NET MVC (great book , by the way) and I have stumbled upon a problem. Note that I'm using MVCContrib, Rhino and NUnit.

    [Test]
    public void ShouldSetLoggedInUserToViewBag() {
        var todoController = new TodoController();
        var builder = new TestControllerBuilder();
        builder.InitializeController(todoController);

        builder.HttpContext.User = new GenericPrincipal(new GenericIdentity("John Doe"), null);

        Assert.That(todoController.Index().AssertViewRendered().ViewData["UserName"], Is.EqualTo("John Doe"));
    }

The code above always throws this error:

System.AccessViolationException : Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

The controller action code is the following:

[HttpGet]
    public ActionResult Index() {
        ViewData.Model = Todo.ThingsToBeDone;
        ViewBag.UserName = HttpContext.User.Identity.Name;

        return View();
    }

From what I have figured out, the app seems to crash because of the two assignements in the controller action. However, I cannot see how there are wrong!?

Can anyone help me pinpoint the solution to this problem.

Thank you.

© Stack Overflow or respective owner

Related posts about c#-4.0

Related posts about nunit