.NET Framework 4.0 Application Runtime Exceptions Problem

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

.NET Framework 4.0 Application Runtime Exceptions Problem

sergvil
Hello,

I have been working with QuantLib since Oct'11 and I think it works great. I used it with .NET Framework 3.5 and it worked perfectly. I use QuantLib and SWIG for C#.

Now I am developing a .NET Framework 4 WPF application with some Quantlib Bond Calculations and I am having some problems:

When I make some testing, each time the app tries to create any QuantLib object or tries to execute any method it throws this exception (click on name for description):

pInvokeStackImbalance

When I try the application on Visual Studio and I have PInvolkeStackImbalance activated on MDA exceptions, I use to receive several PInvoke warnings (green coloured) on each execution, but the application works perfectly. But when I install and I try to execute on Windows, those warnings are exceptions that cause application to crash.

I tried to fix it making some modifications on QuantLib_vc10 project. I found at other forum this solution for the exception we are talking about:

On each dll method call defined on NQuantLibcPINVOKE that throws the exception, for example:

  [DllImport("NQuantLibc", EntryPoint="CSharp_new_TARGET")]
  public static extern IntPtr new_TARGET();


We can avoid the exception adding one parameter:

  [DllImport("NQuantLibc", EntryPoint="CSharp_new_TARGET", CallingConvention = CallingConvention.Cdecl)]
  public static extern IntPtr new_TARGET();


Using this, I can execute the app on Visual Studio with no warnings. But when app is installed on windows I receive other exception:

"The type initializer for 'QuantLib.NQuantLibcPINVOKE' threw an exception."

And the application crashes.


After trying this solution, now I have no idea how to fix it.

Is there any solution? Anybody knows how to compile QuantLib via SWIG in order to execute QuantLib Calculations on .NET Framework 4.0 applications?

If somebody has SWIG-QuantLib dlls (NQuantLib.dll and NQuantLibc.dll) compatible with .NET Framework 4.0 I would be so gratefull if I could receive them.

Thank you in advance.

Best Regards.

Sergio
Reply | Threaded
Open this post in threaded view
|

Re: .NET Framework 4.0 Application Runtime Exceptions Problem

Luigi Ballabio
On Tue, Feb 7, 2012 at 6:03 AM, sergvil <[hidden email]> wrote:
> I have been working with QuantLib since Oct'11 and I think it works great. I
> used it with .NET Framework 3.5 and it worked perfectly. I use QuantLib and
> SWIG for C#.
>
> Now I am developing a .NET Framework 4 WPF application with some Quantlib
> Bond Calculations and I am having some problems:
>
> When I make some testing, each time the app tries to create any QuantLib
> object or tries to execute any method it throws [...]  pInvokeStackImbalance

Hi Sergvil,
    it looks like you're not the only one having this issue.  I
haven't tried it myself, as I don't use C#; but it looks like we have
a bug report for this on Sourceforge:
<http://sourceforge.net/tracker/?func=detail&atid=112740&aid=3466468&group_id=12740>.

The good news is, I was sent a patch recently.  I'm quoting it below;
let me know if it works for you, and I'll try to add it to the
repository (in some way I haven't yet found, since it seems to involve
either modifying SWIG or patching the generated code).

Later,
    Luigi

On Tue, Jan 24, 2012 at 4:22 PM, Mark Gillis <...redacted...> wrote:

> Ahmad’s instructions are very good here:
> http://stackoverflow.com/questions/3334011/compiling-quantlib-via-swig-for-c-sharp,
> which gets you most of the way, but there are some issues when you try to do
> this on VS 2010 and on Windows 7.  Thought I would address those.
>
> First off, I did this all for QuantLib 1.2.   After following Ahmad’s
> instructions above, and getting the completed files etc, and you go to run
> it in your own C# program you get this really nasty error
>  “pInvokeStackImbalance error” (see BigChief’s comment on Ahmad’s post in
> the link above).
>
> This fun one took me a while to figure out, but think I’ve got it nailed
> down.  Its caused by the SWIG conversion from C++ -> C#.  The wrappers that
> SWIG creates doesn’t take into account the correct Calling Conventions into
> account.  The C++ side thinks C# is going to clean up the stack, and the C#
> side thinks the C++ side will clean up the stack, and you end up with this
> ridiculous memory issue when calling any QuantLib methods.
>
> To fix :
>
> 1)      Include windows.i file (found in original SWIG download) into the
> QuantLib-SWIG folder where the .i files are stored before you run SWIG on
> the whole QuantLib folder (as described by Ahmad).
>
> 2)       In the resulting C++ SWIG files, somewhere in there is one that
> says  “# define SWIGSTDCALL __stdcall”  Replace __stdcall with __cdecl (note
> that’s two underscores)
>
> 3)      In the C# side, where all the wrappers are defined with
> [DllImport…], add to each one (use a quick find and replace) the
> CallingConvention = CallingConvention.Cdecl
>
> 4)      This should all build correctly now, just as in Ahmad’s
> instructions.
>
> Note that I’ve only just done this so  I haven’t had a chance to test a lot
> of functionality (beyond now it doesn’t blow up if you try anything), but
> figure it’s a step in the right direction.

------------------------------------------------------------------------------
Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: .NET Framework 4.0 Application Runtime Exceptions Problem

sergvil

Thank you very much!!

The fix works!!

I sent you a previous message saying that it didn't work but I forgot to apply one of the changes described on Ahmad's post.

Now it works perfectly. You can add this fix to QuantLib, it will be very usefull for Framework 4.0 developers.

Thank you again!!

Best Regards.

Sergio
Reply | Threaded
Open this post in threaded view
|

Re: .NET Framework 4.0 Application Runtime Exceptions Problem

Ahmad Mahomed
Hi  Sergio,

Can you please clarify which step  resolved your issue?

Ahmad

On 8 February 2012 04:06, sergvil <[hidden email]> wrote:


Thank you very much!!

The fix works!!

I sent you a previous message saying that it didn't work but I forgot to
apply one of the changes described on Ahmad's post.

Now it works perfectly. You can add this fix to QuantLib, it will be very
usefull for Framework 4.0 developers.

Thank you again!!

Best Regards.

Sergio
--
View this message in context: http://old.nabble.com/.NET-Framework-4.0-Application-Runtime-Exceptions-Problem-tp33276539p33283236.html
Sent from the quantlib-users mailing list archive at Nabble.com.


------------------------------------------------------------------------------
Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users



--
Ahmad Mahomed

------------------------------------------------------------------------------
Try before you buy = See our experts in action!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-dev2
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: .NET Framework 4.0 Application Runtime Exceptions Problem

sergvil

Yes, the second one.

> 2)       In the resulting C++ SWIG files, somewhere in there is one that
> says  “# define SWIGSTDCALL __stdcall”  Replace __stdcall with __cdecl (note
> that’s two underscores)

I forgot to change this code line the first time. After changing it, All worked perfectly.


Ahmad Mahomed wrote
Hi  Sergio,

Can you please clarify which step  resolved your issue?

Ahmad

On 8 February 2012 04:06, sergvil <sergvil@hotmail.com> wrote:

>
>
> Thank you very much!!
>
> The fix works!!
>
> I sent you a previous message saying that it didn't work but I forgot to
> apply one of the changes described on Ahmad's post.
>
> Now it works perfectly. You can add this fix to QuantLib, it will be very
> usefull for Framework 4.0 developers.
>
> Thank you again!!
>
> Best Regards.
>
> Sergio
> --
> View this message in context:
> http://old.nabble.com/.NET-Framework-4.0-Application-Runtime-Exceptions-Problem-tp33276539p33283236.html
> Sent from the quantlib-users mailing list archive at Nabble.com.
>
>
>
> ------------------------------------------------------------------------------
> Keep Your Developer Skills Current with LearnDevNow!
> The most comprehensive online learning library for Microsoft developers
> is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
> Metro Style Apps, more. Free future releases when you subscribe now!
> http://p.sf.net/sfu/learndevnow-d2d
> _______________________________________________
> QuantLib-users mailing list
> QuantLib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/quantlib-users
>



--
Ahmad Mahomed

------------------------------------------------------------------------------
Try before you buy = See our experts in action!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-dev2
_______________________________________________
QuantLib-users mailing list
QuantLib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: .NET Framework 4.0 Application Runtime Exceptions Problem

kcross
Hi All,

Glad my fix for .NET (the cdecl thing...) worked for you guys too.  I'm encountered a bit of a stumbling block though... Everything works fine when I create a new instance of everything, but if I try and update an object (ie update a yield curve) I get a bunch of AccessViolationExceptions (whatever they mean...)

I was wondering if you guys have experienced the same thing etc?  I have a feeling that its something to do with multi-threading, and some garbage collector is acting out of line somewhere, but its been a bit of a nightmare to track...

-kcross

sergvil wrote
Yes, the second one.

> 2)       In the resulting C++ SWIG files, somewhere in there is one that
> says  “# define SWIGSTDCALL __stdcall”  Replace __stdcall with __cdecl (note
> that’s two underscores)

I forgot to change this code line the first time. After changing it, All worked perfectly.


Ahmad Mahomed wrote
Hi  Sergio,

Can you please clarify which step  resolved your issue?

Ahmad

On 8 February 2012 04:06, sergvil <sergvil@hotmail.com> wrote:

>
>
> Thank you very much!!
>
> The fix works!!
>
> I sent you a previous message saying that it didn't work but I forgot to
> apply one of the changes described on Ahmad's post.
>
> Now it works perfectly. You can add this fix to QuantLib, it will be very
> usefull for Framework 4.0 developers.
>
> Thank you again!!
>
> Best Regards.
>
> Sergio
> --
> View this message in context:
> http://old.nabble.com/.NET-Framework-4.0-Application-Runtime-Exceptions-Problem-tp33276539p33283236.html
> Sent from the quantlib-users mailing list archive at Nabble.com.

--
Ahmad Mahomed
Reply | Threaded
Open this post in threaded view
|

Re: .NET Framework 4.0 Application Runtime Exceptions Problem

sergvil
Hello kcross

Can you specify your issue?

This kind of exceptions can be thrown due to different situations.

Perhaps some example code can help to solve your problems.

Regards.
Reply | Threaded
Open this post in threaded view
|

Re: .NET Framework 4.0 Application Runtime Exceptions Problem

Luigi Ballabio
In reply to this post by kcross
May it be related to this?

<http://old.nabble.com/Issues-with-C--Swig-Bindings,-NUnit-and-Settings.instance%28%29.setEvaluationDate%28%29-td30549787.html>

Luigi


On Sat, Mar 10, 2012 at 11:57 PM, kcross <[hidden email]> wrote:

> Glad my fix for .NET (the cdecl thing...) worked for you guys too.  I'm
> encountered a bit of a stumbling block though... Everything works fine when
> I create a new instance of everything, but if I try and update an object (ie
> update a yield curve) I get a bunch of AccessViolationExceptions (whatever
> they mean...)
>
> I was wondering if you guys have experienced the same thing etc?  I have a
> feeling that its something to do with multi-threading, and some garbage
> collector is acting out of line somewhere, but its been a bit of a nightmare
> to track...
>

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: .NET Framework 4.0 Application Runtime Exceptions Problem

kcross
Thanks for the link.  Unfortunately it turned out to be a user error in this case...  I forgot to divide some swap rates by 100....

Do you know if there is a way to have the C# side catch the QuantLib exceptions?  Even if I wrapped it in a try-catch block it wouldn't work..

Cheers

Luigi Ballabio wrote
May it be related to this?

<http://old.nabble.com/Issues-with-C--Swig-Bindings,-NUnit-and-Settings.instance%28%29.setEvaluationDate%28%29-td30549787.html>

Luigi


On Sat, Mar 10, 2012 at 11:57 PM, kcross <markgillis0@gmail.com> wrote:
> Glad my fix for .NET (the cdecl thing...) worked for you guys too.  I'm
> encountered a bit of a stumbling block though... Everything works fine when
> I create a new instance of everything, but if I try and update an object (ie
> update a yield curve) I get a bunch of AccessViolationExceptions (whatever
> they mean...)
>
> I was wondering if you guys have experienced the same thing etc?  I have a
> feeling that its something to do with multi-threading, and some garbage
> collector is acting out of line somewhere, but its been a bit of a nightmare
> to track...
>

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
QuantLib-users mailing list
QuantLib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: .NET Framework 4.0 Application Runtime Exceptions Problem

Luigi Ballabio
On Mon, Mar 12, 2012 at 2:32 AM, kcross <[hidden email]> wrote:
> Do you know if there is a way to have the C# side catch the QuantLib
> exceptions?  Even if I wrapped it in a try-catch block it wouldn't work..

The SWIG machinery should catch the C++ exception and convert it into a C# one.
Doesn't this work? What happens instead?

Luigi

------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: .NET Framework 4.0 Application Runtime Exceptions Problem

kcross
It doesn't seem to want to for some reason.... Any time the c++ dll throws an exception, all the c# side seems to do is throw an AccessViolationException... Even when I throw the code into a try catch block.

Kcross



On Mar 22, 2012, at 4:28 AM, Luigi Ballabio <[hidden email]> wrote:

> On Mon, Mar 12, 2012 at 2:32 AM, kcross <[hidden email]> wrote:
>> Do you know if there is a way to have the C# side catch the QuantLib
>> exceptions?  Even if I wrapped it in a try-catch block it wouldn't work..
>
> The SWIG machinery should catch the C++ exception and convert it into a C# one.
> Doesn't this work? What happens instead?
>
> Luigi

------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: .NET Framework 4.0 Application Runtime Exceptions Problem

Luigi Ballabio
May you check the wrapper code in quantlib_wrap.cpp?  There should be
a try/catch block around each call into QuantLib code that should trap
the exception and convert it into a C# one.  Maybe you can try
debugging that somehow?

Luigi


On Thu, Mar 29, 2012 at 2:52 AM, Mark Gillis <[hidden email]> wrote:

> It doesn't seem to want to for some reason.... Any time the c++ dll throws an exception, all the c# side seems to do is throw an AccessViolationException... Even when I throw the code into a try catch block.
>
> Kcross
>
>
>
> On Mar 22, 2012, at 4:28 AM, Luigi Ballabio <[hidden email]> wrote:
>
>> On Mon, Mar 12, 2012 at 2:32 AM, kcross <[hidden email]> wrote:
>>> Do you know if there is a way to have the C# side catch the QuantLib
>>> exceptions?  Even if I wrapped it in a try-catch block it wouldn't work..
>>
>> The SWIG machinery should catch the C++ exception and convert it into a C# one.
>> Doesn't this work? What happens instead?
>>
>> Luigi

------------------------------------------------------------------------------
Better than sec? Nothing is better than sec when it comes to
monitoring Big Data applications. Try Boundary one-second
resolution app monitoring today. Free.
http://p.sf.net/sfu/Boundary-dev2dev
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users