Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Multi code applications and the clr

Posted on 2007-11-28
7
307 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
ID: 20366207
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
ID: 20381714
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
ID: 20381935
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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 18

Accepted Solution

by:
jcoehoorn earned 125 total points
ID: 20381942
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
ID: 20383337
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
ID: 20383506
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
ID: 31411389
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

792 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