Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"base" equals "this" in overriden method #42

Closed
workerclass opened this issue Aug 16, 2016 · 5 comments
Closed

"base" equals "this" in overriden method #42

workerclass opened this issue Aug 16, 2016 · 5 comments
Assignees
Labels
Milestone

Comments

@workerclass
Copy link

workerclass commented Aug 16, 2016

I have two classes: Base and Child. There is a method in Base that's overriden in Child and that has an Around aspect. Overriden method calls base implementation, but it calls himself instead resulting in StackOverflow exception.
base.GetType() == this.GetType() returns true

@workerclass workerclass changed the title "base" equals this in "base" equals "this" in overriden method Aug 16, 2016
@YuriyIvon
Copy link
Collaborator

Thanks for the report, potentially there might be other side-effects with around aspects and virtual functions. We will analyze all possible issues to provide a reliable solution.

@YuriyIvon
Copy link
Collaborator

I was not able to reproduce StackOverflow issue using a simple example:

    class TraceAspect
    {
        [Advice(InjectionPoints.Around, InjectionTargets.Method)]
        public object Trace(
            [AdviceArgument(AdviceArgumentSource.Type)] Type type,
            [AdviceArgument(AdviceArgumentSource.Name)] string methodName,
            [AdviceArgument(AdviceArgumentSource.Target)] Func<object[], object> target,
            [AdviceArgument(AdviceArgumentSource.Arguments)] object[] arguments)
        {
            Console.WriteLine("Before {0}.{1}", type, methodName);
            var result = target(arguments);
            Console.WriteLine("After {0}.{1}", type, methodName);

            return result;
        }
    }

    class Parent
    {
        public virtual void Print()
        {
            Console.WriteLine("In Parent.Print");
        }
    }

    [Aspect(typeof(TraceAspect))]
    class Child : Parent
    {
        public override void Print()
        {
            base.Print();

            Console.WriteLine("In Child.Print");
        }
    }

I'm a getting correct result when executing this code, though there are some redundant "override" attributes on generated methods which doesn't have corresponding virtual functions in the parent class. That should be fixed, but currently I have no idea how that can lead to the stack overflow issue.

As for base.GetType() == this.GetType() - this condition will always return true, regardless of AspectInjector, because GetType() returns the exact runtime type of the current instance. Since both "base" and "this" belong to the same instance, the call will return the same type in both cases.

Can you provide more details about .NET framework version and which runtime you are using (Microsoft .NET, Mono)?

@workerclass
Copy link
Author

You need to add [Aspect(typeof(TraceAspect))] to Parent class too to get the exception. (Microsoft .NET 4.5.2)

@YuriyIvon
Copy link
Collaborator

I have found the root cause, will publish a fix by tomorrow

YuriyIvon pushed a commit that referenced this issue Aug 18, 2016
@pamidur pamidur added this to the 1.0 rc1 milestone Aug 19, 2016
@pamidur pamidur added the bug label Aug 19, 2016
@pamidur
Copy link
Owner

pamidur commented Aug 31, 2016

Fixed in 1.0.0-beta4

@pamidur pamidur closed this as completed Aug 31, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants