Solved

Multi code applications and the clr

Posted on 2007-11-28
7
309 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
[X]
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
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

752 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