1

I have downloaded and built QuantLib (search google for quantlib and check install link) from their website in visual studio 2010. But because my knowledge of C++ is kind of minimal I would like to use SWIG so I can call the QuantLib libraries with C#. After the SWIG conversion I get a proper build. I thought this would run but at runtime I get the following error on multiple locations in the C# classes:

PInvokeStackImbalance was detected
A call to PInvoke function 'NQuantLib!QuantLib.NQuantLibcPINVOKE::new_Date__SWIG_1' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

MDA Error MSDN

I think that this is a general error so please let me know how to fix this?

SamB
  • 9,039
  • 5
  • 49
  • 56
BigChief
  • 1,413
  • 4
  • 24
  • 37
  • 4
    http://stackoverflow.com/questions/3334011/compiling-quantlib-via-swig-for-c –  Dec 12 '10 at 09:59

4 Answers4

1

It might be some kind of version mismatch.

First: since you're using VC2010, I assume the version of QuantLib and QuantLib-SWIG you're using are checked out from Subversion; is this correct?

Second, did you use the VC2010 projects included in the checkout (the ones with _vc10 in the file name) for both QuantLib and QuantLib-C#, or did you convert other projects? Did you compile both in Release mode? And do the provided C# examples run from the IDE if you tell them to? (They're the EquityOption and BermudanSwaption projects)

Last, what version of SWIG are you using?

Luigi Ballabio
  • 4,128
  • 21
  • 29
  • Hi Luigi. Thanks for your answer. I'm using the SVN QuantLib revision from the 10th of december with boost 1.44. I followed all your steps and building the C# solution in Release mode did the trick. Now it doesn't crash at runtime when running the builds in the example projects. I don´t understand however that when I'm running the examples in Release mode with the debugger attached there are still PInvokeStackImbalance's thrown. Does this matter or can I just remove the PInvokeStackImbalance exception from the debugger exception list without ever looking at it? – BigChief Dec 14 '10 at 20:54
  • http://www.go4answers.com/Example/pinvokestackimbalance-exception-126037.aspx Maybe still a shortcoming in QuantLib because it's not yet fully converted to VS2010? – BigChief Dec 14 '10 at 21:06
  • I see. If that's a problem of calling conventions, I'm afraid there's little we can do within QuantLib since the calls are in the SWIG-generated code. In the meantime, if the last suggestion in the link above works (the one about ) send me the patched projects and I'll add the changes to next release. (If you do that, please check out the latest release first so the diffs will be cleaner.) – Luigi Ballabio Dec 15 '10 at 10:06
  • Hi, I will look at it and send the projects if it works. Thanks for your information. – BigChief Dec 18 '10 at 15:25
  • @BigChief, have you had any luck with that exception? I'm having the same issue where I get "PInvokeStackImbalance's thrown", and I can't seem to find any information anywhere on how to resolve it? (beyond getting buried in the depths of MSDN where I have no idea what's going on...) – keynesiancross Jan 22 '12 at 19:43
1

See http://www.resolversystems.com/products/quantlib-binary/ for a ready built version of QuantLib for C#.

I was up and running in a few minutes.

Contango
  • 76,540
  • 58
  • 260
  • 305
1

Please head to the quantlib extensions page for details on using QuantLib under other languages and platforms.

oers
  • 18,436
  • 13
  • 66
  • 75
ABCD
  • 7,914
  • 9
  • 54
  • 90
0

A temporary fix would be to create a configuration file (App.config) in the QuantLib_vc10 solution and add a NetFx40_PInvokeStackResilience element to suppress the unbalanced stack error.

I created the following App.config file and had this loaded into the BermudanSwaption_vc10 and EquityOption_vc10 projects.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <runtime>
        <NetFx40_PInvokeStackResilience enabled="1"/>
    </runtime>
</configuration>

Then the two projects will work fine, though probably with some cost in performance.

SamB
  • 9,039
  • 5
  • 49
  • 56
mmizutani
  • 317
  • 2
  • 9