3

I have a C# application running under a vendor provided application framework that has a bug in it. I can work around the bug by using reflection to redirect an event handler, but I need to get the instance of the class. I can see the bug from the logging. I don't have source for the application framework and the application framework is no longer supported by the vendor.

I know that memory profilers like redgate can inventory everything in memory so there must be an approach that can get me started.

So how do I grab an instance of an object where I do not have a direct reference?

jeff
  • 3,269
  • 3
  • 28
  • 45
  • This seems like an incredible feet, because to me it sounds like you are asking how to interrogate the heap and the stack in order to retrieve and object from memory....? – Callum Linington Nov 15 '16 at 16:42
  • Built into .NET there really is no way to do this safely. You can pretend to be a debugger, but a process debugging itself rarely ends on a happy note. If it is your class, it's just a class that is managed by a 3rd party library, then you could change your constructor to add the instances somewhere. It could be a path fraught with peril but it would be a safer route if it works. However, other than something like that there is no safe, managed, way to do this. Unsafe code and a good knowledge of the .NET memory structure could work, but this might be prone to changes in the .NET runtime. – Lasse V. Karlsen Nov 15 '16 at 16:42
  • I have no idea if a process can implement the Profiling API itself, but here's documentation on the API: https://msdn.microsoft.com/en-us/library/bb384493(v=vs.110).aspx#profiling_api –  Nov 15 '16 at 16:42
  • I'd inject some sort of registration code into the constructor(s) using [CodeInject](https://github.com/punitganshani/codeinject) – spender Nov 15 '16 at 16:55
  • Maybe it will help if you publish the code in question (that buggy class) and what exactly you need to redirect. I mean maybe it can be done the other way without getting references to all objects. – Evk Nov 17 '16 at 06:55
  • Actually, i'm only interested in getting a reference one object. But that one is floating around in the third party COTS framework. – jeff Nov 19 '16 at 16:41

0 Answers0