SharpCrafters Forum – Attempted to read or write protected memory

SharpCrafters Support Forum

        


Attempted to read or write protected memory Expand / Collapse
Author
Message
Posted 11/27/2009 2:30:56 PM


Pierre-Emmanuel Dautreppe

Community Member
Hello

I have this error when I mix several aspect :
"AccessViolationException : Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

I am currently mixing 3 aspects. When I comment one of those it works correctly.

First I will inject an interface on an object :
  public interface IObservable { }

  [Serializable]
  internal class ObservableImplementation : IObservable
  {
  }

  [Serializable]
  internal class ObservableAttribute : CompositionAspect
  {
     public override Type GetPublicInterface(Type containerType)
     {
        return typeof(IObservable);
     }

     public override object CreateImplementationObject(InstanceBoundLaosEventArgs eventArgs)
     {
        return new ObservableImplementation();
     }

     public override CompositionAspectOptions GetOptions()
     {
        CompositionAspectOptions options = base.GetOptions();
        options |= CompositionAspectOptions.GenerateImplementationAccessor;
        return options;
     }
  }


I also want to inject some code in my property setters so I have declared the following aspect :
  [Serializable]
  [MulticastAttributeUsage(MulticastTargets.Method, AllowExternalAssemblies = true, Inheritance = MulticastInheritance.Multicast)]
  internal partial class TrackPropertyChangesAttribute : OnMethodBoundaryAspect
  {
     public override bool CompileTimeValidate(MethodBase method)
     {
        return method.Name.StartsWith("set_");
     }
  }


And I set these aspects to my object :
  [Observable]
  [Serializable]
  [TrackPropertyChanges]
  public abstract class ObjetObservable
  {
  }


Finally I decalre another aspect that will inject a new field in my classes (but that does nothing for now):
  [Serializable]
  [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
  [MulticastAttributeUsage(MulticastTargets.Field)]
  [RequirePostSharp("InjectFieldLoadedPlugin", "InjectFieldLoadedTask")]
  public class LazyLoadingAttribute : CompoundAspect
  {
     public override void ProvideAspects(object element, LaosReflectionAspectCollection collection)
     {
        collection.AddAspect(element, new ImplementInjectFieldAspect());
     }
  }

<?xml version="1.0" encoding="utf-8" ?>

<PlugIn xmlns="http://schemas.postsharp.org/1.0/configuration">
  <SearchPath Directory="bin/{$Configuration}"/>
  <TaskType Name="InjectFieldLoadedTask" Implementation="Framework.Data.DataAccessLayer.Aspects.LazyLoading.InjectFieldLoadedTask, ConsoleApplication1" />
</PlugIn>

  [Serializable]
  internal class ImplementInjectFieldAspect : ILaosFieldLevelAspect
  {
     void ILaosFieldLevelAspect.RuntimeInitialize(FieldInfo field) { }
  }

  internal class InjectFieldLoadedTask : Task, ILaosAspectWeaverFactory
  {
     LaosAspectWeaver ILaosAspectWeaverFactory.CreateAspectWeaver(AspectTargetPair aspectSemantic)
     {
        if ( aspectSemantic.IsDerivedFrom(typeof(ImplementInjectFieldAspect)))
           return new InjectFieldLoadedWeaver();

        return null;
     }
  }

  internal class InjectFieldLoadedWeaver : FieldLevelAspectWeaver
  {
     public InjectFieldLoadedWeaver() : base(null) { }

     public override void Implement()
     {
     }
  }


Note that I want my attribute to be a Compound because I will in the future add another aspect in it.

Note also that when I was having all my "code generation" code in my aspects, I could see that my classes where correcty processed and updated.

Finally I add a versy simple code :

  public class UnitTest1
  {
     static int Main(string[] args)
     {
        var objet = new MonDossierAvecUnPret_Observable();
        objet.AutrePropriete = 15;

        return 0;
     }

     public class MonDossierAvecUnPret_Observable : ObjetObservable
     {
        public int AutrePropriete { get; set; }

        [LazyLoading]
        public MonPretObservable Pret;
     }
     public class MonPretObservable : ObjetObservable { }

  }


And I get the exception when I run it on my constructor. The exception comes from the InitializeAspect method.
In my production code, it was coming from <>AspectsImplementationDetails_1..cctor.

Do you have any idea of what's happening ?

For info, I was using the 1.5.6.690 but I have the same behavior with 1.5.6.761
Post #2579
Posted 11/27/2009 2:34:20 PM


Gael Fraiteur

SharpCrafters
What is the full exception stack?
Post #2580
Posted 11/27/2009 2:36:09 PM


Pierre-Emmanuel Dautreppe

Community Member
There is none...
Post #2581
Posted 11/27/2009 2:38:57 PM


Gael Fraiteur

SharpCrafters
Ah it was at runtime, not build time.

Try to attach a debugger and catch any exception to see what happens.

-g
Post #2582
Posted 11/27/2009 2:42:08 PM


Pierre-Emmanuel Dautreppe

Community Member
Yes sorry, at runtime.
I cannot get any error.

In VS, I need to go in assembler code to see where it fails and I cannot get a more precise error.

I have tried to use Deblector to debug in the reflector but I'm not so used to this tool and I have no more precised error.

I continue to investigate
Post #2583
Posted 11/27/2009 2:44:20 PM


Gael Fraiteur

SharpCrafters
Yes, but if you can break into the debugger, you can see the stack.
Post #2584
Posted 11/27/2009 2:53:54 PM


Pierre-Emmanuel Dautreppe

Community Member
Not really no..?
The only thing I can get in my code is
"{"The type initializer for '<>AspectsImplementationDetails_1' threw an exception."}"
when calling one of my aspected property.
Post #2585
Posted 11/27/2009 2:58:21 PM


Gael Fraiteur

SharpCrafters
So I don't know. You can submit a repro to the issue tracker and i'll have a look.
Post #2586
Posted 11/27/2009 3:11:30 PM


Pierre-Emmanuel Dautreppe

Community Member
OK I have attached the solution.
<!-- m --><a class="postlink" href="http://www.postsharp.org/tracker/view.php?id=424">http://www.postsharp.org/tracker/view.php?id=424</a><!-- m -->
Post #2587
Posted 1/22/2010 3:29:31 PM


Pierre-Emmanuel Dautreppe

Community Member
Hello Gaël,

Have you been able to have a look to this problem ?
Post #2588
Posted 1/25/2010 7:11:45 AM


Gael Fraiteur

SharpCrafters
Not yet.
Post #2589
« Prev Topic | Next Topic »


All times are GMT +1:00, Time now is 11:45am

Powered By InstantForum.NET v4.1.4 © 2010
Execution: 0.136. 7 queries. Compression Disabled.