Instances of ExcelRtdServer never being Garbage Collected

Feb 24, 2015 at 1:47 PM
Edited Feb 24, 2015 at 1:49 PM
Hello Govert,
Using ANTS memory profiler I am seeing that my simple subclass of ExcelRtdServer (there is no functionality just inheritance) is never GCed after all relevant Topics are deleted.

To verify I did the following -
  1. Added a formula to the sheet which in code calls XlCall.RTD("simplertd") - this causes my Subclass to be created, ServerStart and then ConnectData to be called
  2. Delete this formula - this causes DisconnectData and then ServerTerminate to be called.
    Repeating this process many times I observe the Subclass is recreated each time. If I then view the managed memory in ANTS it shows many instances of ExcelRTDServer rooted into many instance of SingletonClassFactory.
The reason I am investigating this is because we have observed increasing unmanaged memory when our ExcelRtdServer class is recreated and non-increasing unmanaged memory if we leave one formula on the sheet which causes our ExcelRTDServer instance to be persisted.

Do you have any ideas? Thanks in advance.
Value2
Coordinator
Feb 24, 2015 at 4:28 PM
What is the exact version of Excel that you are using?
Excel 2010 RTM had some RTD--related bugs that Excel-DNA is not yet fompletely handling.

Excel 2010 SP1 and other versions should be fine.

-Govert



Feb 25, 2015 at 9:06 AM
Hello Govert,

The version is 14.0.7128.5000.
Additional Version Excel 2010 (14.0.7140.5000) SP2 MSO (14.0.7128.5000)

Thanks for the assistance.
Value2
Coordinator
Feb 25, 2015 at 1:23 PM
It should be fine with that version of Excel.

Does the ServerTerminate get called correctly?

I can only have a closer look in two weeks' time.

-Govert



Feb 25, 2015 at 1:30 PM
Hi Govert,

The ServerTerminate does get called correctly.

Thanks,
Value2
Coordinator
Feb 25, 2015 at 1:38 PM
Are you checking with the Debug version of Excel-DNA or a release version. The debug version might run the workaround code for Excel 2010 RTM in all cases.

Otherwise you'll have to figure out what the GC root is that keeps it alive.

-Govert



Feb 26, 2015 at 1:32 PM
Edited Feb 26, 2015 at 1:38 PM
Govert,

Your theory was correct. I built the latest source in debug mode and the issue was resolved.
Further I narrowed down the issue to the DEBUG preprocessor directive in the static constructor of ExcelRtd2010BugHelper.

Therefore changing it to
 static ExcelRtd2010BugHelper()
        {
            // RTM was:                                     14.0.4760.1000
            // the Excel version with the hotfix* is:       14.0.5128.5000
            // SP1 where the bug is also fixed has version: 14.0.6129.5000
            // * according to http://support.microsoft.com/kb/2405840
            ExcelVersionHasRtdBug = true;   // To test a bit...
        }
allows you to build in release and not have the memory leak.

Thanks for you help,
Value2.
Coordinator
Feb 26, 2015 at 1:47 PM
Sorry - I'm confused. So you mean there is a problem with the Release version as is?

I would expect the problem to be present only if you have
 ExcelVersionHasRtdBug = true;
actually set.

Since your excel version does not have the RTD bug, it should be false and then work correctly.
Otherwise there might be some other problem, like a cross-thread COM call going wrong, and I'd be interested to know.

-Govert


Feb 26, 2015 at 1:57 PM
Govert,

Yes, there is an issue with the release version as is with our Excel build.
I would expect the problem to be present only if you have
ExcelVersionHasRtdBug = true;
actually set.
This actually solves the problem.

Thanks,
Value2
Coordinator
Feb 27, 2015 at 3:22 AM
OK - I'll have to take a closer look.

If you can mail me a small self-contained project that exhibits the problem, it would help me a lot.

-Govert



Feb 27, 2015 at 7:11 AM
Edited Feb 27, 2015 at 7:23 AM
Govert,
Below are the two classes I used to demonstrate this plus the latest version of ExcelDna downloaded from this site.
If you need any more than this please reply.
public class Class1
    {
        [ExcelFunction(IsMacroType = true)]
        public static object func()
        {
            var a = "asdf";
            XlCall.RTD("ExcerDnaTest.Server", null, a);
            return new object();
        }

        [ExcelFunction(IsMacroType = true)]
        public static object gc()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            return new object();
        }
    }

    public class Server : ExcelRtdServer
    {
        protected override bool ServerStart()
        {
            return base.ServerStart();
        }

        protected override object ConnectData(Topic topic, IList<string> topicInfo, ref bool newValues)
        {
            return base.ConnectData(topic, topicInfo, ref newValues);
        }

        protected override void DisconnectData(Topic topic)
        {
            base.DisconnectData(topic);
        }

        protected override void ServerTerminate()
        {
            base.ServerTerminate();
        }
    }
Thanks,
Value2
Coordinator
Feb 27, 2015 at 7:46 AM
That's perfect, thank you.

I'll post back once I've had a chance to dig a bit further.

-Govert



Mar 5, 2015 at 7:17 AM
Edited Mar 5, 2015 at 7:19 AM
Hello Govert,
Further information to this.
As discussed we enabled
ExcelVersionHasRtdBug = true;
which caused our instance of the ExcelRtdServer to be GCed, however, on closer inspection we found that the ExcelRTD2010BugHelper class is now not being GCed instead. I have represented below the ants profiler diagram showing how the class is hooked in. Worth mentioning that this shows the ExcelRTD2010BugHelper hooked in the same way our ExcelRtdServer was.

4 instances
SingletonClassFactory -> ExcelDna.Integration.ExcelRTD2010BugHelper

Thanks,
Value2
Coordinator
Apr 1, 2015 at 9:59 PM
Hi Value2,

I've finally had a chance to look at this - I can reproduce what you report where the ExcelRtdServer object is never collected.

It looks like something is going wrong with the COM class factory registration / deregistration. I might have to dig into my COM books to figure out what I'm doing wrong here.

Thanks for reporting the issue.
I'll update again when I've made some progress.

-Govert
Coordinator
Apr 1, 2015 at 10:06 PM
I've also added an issue on GitHub for this: https://github.com/Excel-DNA/ExcelDna/issues/5
Coordinator
Apr 23, 2015 at 10:25 AM
Edited Apr 23, 2015 at 10:26 AM
This issue should be fixed in that latest source version on GitHub (https://github.com/Excel-DNA/ExcelDna). I made a few changes to the RTD and other COM registration, so any feedback on whether this has fixed the problem, or introduced any other, would be greatly appreciated. I have done very limited testing so far.
Jun 26, 2015 at 2:47 PM
Hello Govert,

Can confirm this fixed the issue.
Thanks for your help.

Value2