Solved

Multi code applications and the clr

Posted on 2007-11-28
7
303 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

896 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

13 Experts available now in Live!

Get 1:1 Help Now