VS2010 for debugging com interop memory leaks - visual-studio-2010

I know there have been some major improvements in Visual studio 2010 for debugging things like memory leaks from dump file etc. I wonder if it has any improvements specifically debugging memory leak for COM interop scenarios?

There's no specific way to test for memory leaks in unmanaged code in VS, especially from a managed host point of view. It gets even worse if you have no access to the unmanaged code.
My suggestion would be to create a bunch of instances of the COM object and repeatedly call its methods then dispose of the objects. If the memory usage just keeps increasing, there's probably a memory leak.
Then the real trick is to find it! There are a lot of ways to do this (way too many to enumerate here) so I'd suggest a quick Google for memory leak debugging tools.

Related

Memory Leak Issue in Windows Phone Develoment - Silver Light Framework

I am creating one game in Windows phone using c# and silver light platform. I am new in this technology and currently facing memory leak issue.
As per research and study I have done, I have tried to do all the things including events, string and usage of garbage collector.
Can any one please give common tips to best utilize garbage collector and memory management since it seems issue right now. When my garbage collector reaches 5 lac size, it stop collecting new things and application is getting crash.
I also tried empty the garbage collectore passing parameter 0 in gc collect but it is crashing the app.
Can you please guide and help for basic things to take care, process to follow to avoid such issues and best use of GC collect?
Thanks in advance,
Jacob
In general, you should never have to call GC.Collect yourself as unused objects will be automatically collected every few seconds.
As for what can prevent objects from being collected, it comes down to them being "rooted". Roots include:
Any static references
Any references held by the run loop (your Application is the closest thing here)
Anything being displayed on the current page or any page behind it
Anything referenced by any of the above (including UI events), or referenced by anything that is referenced by any of the above (etc).
In the above scenarios, those objects and any objects they hold a reference to cannot be GC'd. So as for advice:
Avoid defining anything as static
Be careful how many objects are held by Application
Avoid a navigation model that allows your back stack to grow to ulimited levels
Potentially look at setting references to large data sets to null in your page/viewmodel's OnNavigatedFrom method and re-initialise them in OnNavigatedTo
I'd recommend using the Windows Phone Profiler, which comes with the 7.1 SDK. It will tell you what objects are in memory and why.
Without seeing any of your code, it is difficult to give specific advice.
However, I strongly suggest you run a memory profiling tool like ANTS Memory Profiler or .Net Memory Profiler. These tools will show you what portions of your code are never released and are very helpful in making the adjustments that you need.

How can I disable memory leak logging in Visual Studio 2005?

I would like to know if there is a way to disable the built-in memory leak logging that Visual Studio (2005, if it matters) does when ending a process.
When I'm debugging a big piece of code, I might want to only test a specific feature before investigating leaks. My problem is that when I kill the process, leak logging takes a lot of time. Sometimes I can wait more than a minute for all the dumping to take place.
Thank you very much
The function CrtDumpMemoryLeaks is called when your program is compiled in debug mode. You can turn it off this behaviour with a call to the function
_CrtSetDbgFlag(0);
Its usage is described here. But fixing those memory leaks would still be wise. Normally it some big static objects hold all the memory. You can refactor them from static objects declared in a cpp file by using enclosing them inside a class which creates the object at first access in the getter function. Now you can safely delete your statics at program exit as last call in your main method. That does fix the memory leak and your report will be much shorter.
Yours,
Alois Kraus

How to analyse program's memory usage in Visual Studio 2010?

Is there a way to check what memory has been allocated to in a program coded in VS 2010?
I've been noticing that a recent program I've been making keeps using more and more memory over time without releasing any.
Also, are there any articles on code design that teaches you good memory management? I seem to be searching the wrong terms on google as I keep getting useless results.
There are commercial tools such as ANTS Memory Profiler or .NET Memory Profiler or dotTrace. Secondly, take a look at this MSDN article. Lastly, Maoni Stephens has many blog posts on garbage collection and memory management in .NET. They are all linked here.

Can you start and stop boundschecker (DevPartner)?

I'm trying to use boundschecker to analyze a rather complex program. Running the program with boundschecker is almost too slow for it to be of any use since it takes me almost a day to run the program to the point in the code where I suspect the issue exists. Can anyone give me some ideas for how to inspect only certain parts of my software using boundschecker (DevPartner) in Visual Studio 2005?
Thanks in advance for all your help!
I last used BoundsChecker a few years ago, and had the same problems. With large projects, it makes everything run so slowly that it is useless. We ended up ditching it.
But, we still needed some of it's functionality, but like you, not for the whole program. So we had to do it ourselves.
In our case, we mainly used it to try and track down memory leaks. If that's your objective as well, there are other options.
Visual Studio does a pretty good job of telling you about memory leaks when your program exits
It reports leaks in the order that they were created
It will tell you exactly where the leaked memory was created if your source files have this at the top
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
Those help a lot, but it's often not enough. Adding that snippet everywhere isn't always feasible. If you use factory classes, knowing where memory was allocated doesn't help at all. So when all else fails, we take advantage of #2.
Add something like the following:
#define LEAK(str) {char *s = (char*)malloc(100); strcpy(s, str);}
Then, pepper your code with "LEAK("leak1");" or whatever. Run the program, and exit it. Your new leaked strings will display in Visual Studio's leak dump surrounding the existing leaks. Keep adding/moving your LEAK statements and re-running the program to narrow your search until you've pinpointed the exact location. Then fix the leak, remove your debugging leaks, and you're all set!
BoundsChecker tracks all memory allocations and releases in extreme detail. It knows, for instance, that such and such a memory allocation was done from the C runtime heap, which in turn was taken from a Win32 heap, which in turn started life as memory allocated by VirtualAlloc. If the application was instrumented (FinalCheck), it also has detailed information as to which pointers reference the memory.
This is one reason (of many) why the thing is slow.
If BC were to connect to an application late, it would have none of this data built up, and would have either (1) dig it all up at once, or (2) start guessing about things. Neither solution is very practical.
One way to lighten up BoundsChecker is by excluding from instrumentation all but the few modules you are interested in. I know thats not great because if you knew where the leak was you wouldn't need BoundsChecker. What I usually recommend is that you use BC's Active Check mode first with only Memory Tracking available. You miss the API Validations but you could always rerun that seperately. After you run Active Check and you get clues regarding which modules tend to be problematic, only then do you enable instrumentation for the module or modules of interest and their dependencies. We know Final Check is annoyingly slow but as Mistiano correctly states, with Final Check not only does BC keep a graph of all allocated blocks but also all pointers and contexts to them. Therein lies the magic in how Final Check can nail leaks and corruptions at the point of occurance, not just on application shutdown or fault. Shameless plug: I work on the DevPartner team. We are releasing DPS 10.5 on February 4, 2011 with x64 application support in BC. Unlike the relatively ancient and undersold BC64 for Itanium which only provided Active Check, DPS 10.5 provides full Final Check support for x64 applications, both for pure C++ and for native modules running in .NET processes. See microfocus.com under MF Developer for details.

Debugging crash and memory leak

We are developing a plugin for outlook in visual basic 6.0.We have been facing issues like outlook crash ( due to some objects not getting destroyed or some other reason). One way we have been solving this issue is reading code,commenting code( to observe the affect), logging etc. Are there any other ways of debugging? Are there tools which will help us in knowing what objects are held in memory?
You can look into the Debugging tools for Windows.
They take a little time to get used to, but the crash dumps can be very helpful...

Resources