Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9251535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:43:33+00:00 2026-06-18T10:43:33+00:00

I am using the following interface as a ToFactory() binding: public interface ISamplerFactory {

  • 0

I am using the following interface as a ToFactory() binding:

public interface ISamplerFactory
{
    ISampler Create(Action<EventHandler<ValueChangedEventArgs>> register, Action<EventHandler<ValueChangedEventArgs>> unregister, Func<decimal?> valueGetter);
}

When I bind ToFactory() I can successfully create the class but I then get a memory leak whereby the register, unregister and valueGetter parameters are held by a ConstructorArgument inside Ninject, which reference a target/parameter object inside the delegates. This keeps that target object from getting GC’d. I am using ContextPreservation extension too if that makes a difference. (See complete sample code below)

When I remove the “ToFactory()” bind and create a standard factory class, it works.

public class SamplerFactory : ISamplerFactory
{
    private readonly IDistributionResolver _resolverFactory;

    public SamplerFactory(IDistributionResolverFactory resolverFactory)
    {
        _resolverFactory = resolverFactory;
    }

    ISampler Create(Action<EventHandler<ValueChangedEventArgs>> register, Action<EventHandler<ValueChangedEventArgs>> unregister, Func<decimal?> valueGetter)
    {
        return new SpecificSampler(_resolverFactory, register, unregister, valueGetter);
    }
}

And my target objects inside the delegates are GC’d successfully.

Is there something I am doing wrong or isn’t the Factory extension meant to handle these more complex arguments? I assume if I used the .WithConstructorArgument I would get the same outcome.

EDIT: Added all necessary bindings and rewrote my sample code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    using System;
    using Ninject;
    using Ninject.Extensions.ContextPreservation;
    using Ninject.Extensions.Factory;

    public class Program
    {
        static void Main(string[] args)
        {
            new Program().Run();
        }

        public void Run()
        {
            var kernel = new StandardKernel(new NinjectSettings() { LoadExtensions = false });
            kernel.Load(new FuncModule());
            kernel.Load(new ContextPreservationModule());

            kernel.Bind<IDistributionResolver>().To<DistributionResolver>(); // This is a constructor-less object.
            kernel.Bind<ISampler>().To<SpecificSampler>();
            kernel.Bind<ISamplerFactory>().ToFactory();
            kernel.Bind<IInjected>().To<Injected>();
            kernel.Bind<IDistributionResolver>().To<DistributionResolver>();
            kernel.Bind<IDistributionResolverFactory>().ToFactory();


            var s = new SomeObject();
            var weakS = new WeakReference(s);

            var factory = kernel.Get<ISamplerFactory>();

            var sampler = CreateInstance(factory, s);

            s = null;
            factory = null;
            sampler = null;

            GC.Collect();

            if (weakS.IsAlive)
                throw new Exception();
        }

        private ISampler CreateInstance(ISamplerFactory factory, SomeObject someObject)
        {
            var x = factory.Create(y => someObject.Do += y, z => someObject.Do -= z, () => someObject.Query());

            if (x == null)
                throw new Exception();

            return x;
        }


        public class SomeObject
        {
            public event EventHandler<ValueChangedEventArgs> Do;

            public decimal? Query()
            {
                return 0;
            }
        }

        public class SpecificSampler : ISampler
        {
            private readonly IDistributionResolverFactory resolver;
            private readonly Action<EventHandler<ValueChangedEventArgs>> register;
            private readonly Action<EventHandler<ValueChangedEventArgs>> unregister;
            private Func<decimal?> _valueGetter;

            public SpecificSampler(
                IDistributionResolverFactory resolver, // This is injected
                Action<EventHandler<ValueChangedEventArgs>> register, // The rest come from the factory inputs
                Action<EventHandler<ValueChangedEventArgs>> unregister,
                Func<decimal?> valueGetter)
            {
                this.resolver = resolver;
                this.register = register;
                this.unregister = unregister;
                _valueGetter = valueGetter;
                // Do Stuff;
            }
        }

        public class ValueChangedEventArgs : EventArgs
        {
        }

        public interface ISamplerFactory
        {
            ISampler Create(Action<EventHandler<ValueChangedEventArgs>> register, Action<EventHandler<ValueChangedEventArgs>> unregister, Func<decimal?> valueGetter);
        }

        public interface IDistributionResolverFactory
        {
            IDistributionResolver Create(IDictionary<string, string> picked);
        }

        public interface IDistributionResolver
        {

        }

        private class DistributionResolver : IDistributionResolver
        {
            readonly IInjected _i;
            readonly IDictionary<string, string> _picked;

            public DistributionResolver(IInjected i, IDictionary<string, string> picked)
            {
                _i = i;
                _picked = picked;
            }
        }

        public interface ISampler
        {
        }
    }

    public interface IInjected
    {
    }

    class Injected : IInjected
    {
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:43:34+00:00Added an answer on June 18, 2026 at 10:43 am

    I tracked this down to a memory leak in Ninject Core:

    https://github.com/ninject/ninject/issues/74

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to register the following class using the fluent interface: public class
Using the JavaScript Native Interface of GWT I can perform the following: public native
I have following interface public interface IBuilder<T> { T Create(string param); } with many
I have implemented the repository pattern using the following generic interface. public interface IRepository<T>
I'm using dependency injection and have the following interface: public interface IAchievement : IEquatable<IAchievement>
I am using following code to create the paint brush in iphone. @interface Canvas
In our web.config I am using the following tag to determine the interface language
I'm trying to get the Global Interface Table by using the following code (Delphi):
Say I am using the following interface (assume we can't change our implementation to
Let's say that I have the following interface: public interface IMyService { void SimpleMethod(int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.