Solved

Multi code applications and the clr

Posted on 2007-11-28
7
302 Views
Last Modified: 2013-12-16
Hi.

I've a multi-code app, with the executable and dlls all built within Visual Studio 2005. The 'chain' is a VB front-end, C# dll, and a VC++ dll.

The app builds and runs fine in debug mode, and in the VC++ dll, again the whole app runs perfectly well whether the VC++ dll is built as managed code (with the clr compile arg) or unmanaged.

However, in release mode not all is well. If the VC++ dll is built as managed the app does run fine, but when I remove the clr compilation args so the dll is unmanaged, the app crashes with no dump file. I've used lint to check for any memory-leaks, race conditions, buffer overruns etc in the VC++ dll but all was fine. This is a high-frequency application, so ideally I need the VC++ dll to be unmanaged. The speed difference between managed and unmanaged is too great to just settle for managed.

Once the VC++ dll has finished its processing, it calls a C# delegate via a function ptr.

Has anyone else experienced this problem with multicode apps or, is using unmanaged code for this purpose not recommended/possible?

Thanks.
0
Comment
Question by:oliversk
  • 4
  • 3
7 Comments
 
LVL 18

Expert Comment

by:jcoehoorn
Comment Utility
Add logging capabilities to your C++ dll, so you can get a picture of where things are as it executes in release mode and at what point it crashes.
0
 

Author Comment

by:oliversk
Comment Utility
Hi jcoehoorn.

I'd already done that, and whilst it was never the same line of code every time, it was always around the function ptr call to the C# delegate.

But again, no crash file. In debug builds (wth and without the clr) no problems, release mode with clr no problem, but release mode without the clr a crash.
0
 
LVL 18

Expert Comment

by:jcoehoorn
Comment Utility
I'm wondering if maybe your C# delegate isn't property 'pinned'.  In unmanaged C++, when you get a pointer to something all you have is an address.  In managed C#, the framework is free to move things around as objects go in and out of scope.  So an unmanaged pointer to a managed object of any type, including functions, could easily become invalid.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 18

Accepted Solution

by:
jcoehoorn earned 125 total points
Comment Utility
That would explain why managed C++ works- the framework will make sure the pointer is valid.  It also explains why debug mode works, but then the framework is less likely to re-arrange things on you.
0
 

Author Comment

by:oliversk
Comment Utility
This is looking promising.

I've now pinned the delegate:

Callback = new Communication.MESSAGECALLBACK(WrapperCallBack);
CallbackPinned = GCHandle.Alloc(Callback, GCHandleType.Pinned);
Communication.SetCallbackDelegate(Callback);

It all compiles, but when I run the app it crashes on the GCHandle line: "Object contains non-primitive or non-blittable data."

As you can tell I'm still a novice when it comes to .NET so any other assistance you can give is again very much appreciated.

Thanks.

0
 

Author Comment

by:oliversk
Comment Utility
I've now also tried
CallbackPinned = GCHandle.Alloc(Callback);

This works for 1 callback, but then crashes/exits on all subsequent callback attempts. This is much like the behaviour experienced beforehand without GCHandle.

Thanks again.
0
 

Author Closing Comment

by:oliversk
Comment Utility
Got me on my way to reading more about garbage collecting with the CLR where I found the correct way to declare my delegate.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now