6

I have a series of NUnit tests and some are failing, yet I can't seem to find a reason, and the exception is telling me nothing. This is my case:

    //Controller Action
    [HttpPost]
    [AjaxExceptionHandler]
    [OutputCache(Duration = 0)]
    public PartialViewResult SomeAction(long id)
    {
        try
        {
            var model = _repository.GetModel(id);
            return PartialView(@"MyPartialView", model);
        }
        catch (Exception ex)
        {
            exceptionManager.HandleException(ex, FT_EXCEPTION_POLICY);
            throw;
        }
    }

    //Action Unit Test
    [Test]
    [Category(TestConstants.UnitTest)]
    public void SomeAction_Returns_Expected_View()
    {
        var model = Builder<ViewModel>.CreateNew().Build();

        repository.Stub(it => it.GetModel(Arg<long>.Is.Anything)).Return(model);

        var viewResult = (PartialViewResult)someController.SomeAction(1);
        Assert.AreEqual(@"MyPartialView", viewResult.ViewName);
    }       

Unit Test Exception:

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

If in my action I pass a null value to the partial view, like so: return PartialView(@"MyPartialView", null); Then the test passes.

Other similar cases fail as well, yet others pass. I have not been able to identify a reason for each.

Can anyone help me identify what is wrong?

Thanks,

EDIT: Ok, I fixed ALL other failing tests and now I have only the ones with the System.AccessViolationException left.

ADDED Setup procedure form my tests:

    [SetUp]
    public void SetUp()
    {
        controllerBuilder = new TestControllerBuilder();

        repository = MockRepository.GenerateStub<ISomeRepository>();

        someController = new SomeController
            (repository);

        controllerBuilder.InitializeController(someController);
    }
AJC
  • 1,853
  • 1
  • 17
  • 28
  • Does this test pass if you remove outputchache attribute? – David Sep 10 '11 at 19:09
  • @Dvd - Other actions with the same attribute pass without problems... But I'll try... Right now I'm just shooting in the dark, see what hits. EDIT: Nop... same result. – AJC Sep 10 '11 at 19:23
  • Just to be certain, does this test fail when run in isolation or only when the entire suite is run? – Lieven Keersmaekers Sep 10 '11 at 19:32
  • @Lieven - both cases... Now I am finding that some other tests that failed for other reasons, when fixed now fail with this same exception... Thank god pcs are not nintendo controls, cause mine would be broken by now... – AJC Sep 10 '11 at 20:20
  • Lets try to shoot at the moon. Did you tests works before? Have you done any changes in View, binding, or resources? Did you miss some of the resources required by the controls attached to that VM? – Alan Turing Sep 10 '11 at 21:20
  • @Artur - Nop. Some tests pass, other fail (Consistently). From those who fail, some where for other reasons, but those are fixed. However some of those who were fixed now fail for the exception in question, (So never really passed). The test are pretty consistent. – AJC Sep 10 '11 at 21:35

3 Answers3

3

Found an answer... really stupid problem, like most problems in programming. As I always say, if you can't solve it the first couple of hours, then you know its something really really stupid.

Here is where I found the answer, took me a while, but the name of the question didn't help things either:

Attempted to read or write protected memory

In short, I had to replace the MVCContrib Dlls.

Thanks to everyone for the help...

Community
  • 1
  • 1
AJC
  • 1,853
  • 1
  • 17
  • 28
0

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

This error comes in a field when playing with managed/unmanaged code, especially allocating unmanaged resources in managed code and freed them too early, when managed code still exists to access resources being released by the operating system.

You are using IntPtr inpropertly, or have a memory leak, or declaration of an extern COM/Win32 functions is not correct, f/e in [DllImport(...)] attributes.

Look into the code more criticall & presizely

exceptionManager.HandleException(ex, FT_EXCEPTION_POLICY);
Alan Turing
  • 2,482
  • 17
  • 20
  • Thanks for your response... The exception handler is used in most my actions and many of them pass. Also, I have a Unit Test case for when the exception is thrown and it passes correctly. – AJC Sep 10 '11 at 19:21
  • Commented out the try catch, same result... What gets to me its that it is a very simple code... – AJC Sep 10 '11 at 19:26
0

It could be a threading / race condition problem.

You are not creating the controller, somecontroller in the test. Therefore, many tests are using the same instance of the controller, this can lead to memory corruption errors.

Try creating and disposing the controller inside each test.

Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252