Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28469 Discussions

Managed Debugging Assistant PInvokeStackImbalance

CharlieG
Beginner
2,598 Views
I'm starting to use Visual Studio 2010 and Intel Visual Fortran Composer XE 2011 on an existing code base. I'm getting an error that I haven't seen before:

Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in '...\\FCCMod.exe'. Additional Information: A call to PInvoke function 'FCCMod!FCCMod.clsFCCDll::FCCMod' 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 unmanged signature.

Seems like they're trying to tell me what the problem is but I'm not a knowledgable enough programmer to understand the message. If anyone could help me out I'd appreciate it.

My Visual Basic Code:

Public Class clsFCCDll

'FCC Model connections

Public Declare Sub FCCMod Lib "FCCmod.dll" Alias "FCCMod" (ByVal CommonUserDataPath As String, ByVal ExePath As String)

Public Sub FCCModel(ByVal CommonUserDataPath As String, ByVal ExePath As String)

FCCMod(CommonUserDataPath, ExePath)

End Sub

End Class

My Visual Fortran Code:

!DEC$NOFREEFORM

subroutine FCCMod(strUserAppPath, strExePath)

!DEC$ATTRIBUTES ALIAS:'FCCMod'::FCCMod

!DEC$ATTRIBUTES DLLEXPORT :: FCCMod

use IFCOM

use IFCOMTY

use IFWIN

CHARACTER(256), INTENT(IN) :: strUserAppPath

!DEC$ ATTRIBUTES REFERENCE :: strUserAppPath

CHARACTER(256), INTENT(IN) :: strExePath

!DEC$ ATTRIBUTES REFERENCE :: strExePath

status = AllocConsole()

ILEN = INDEX(strUserAppPath,CHAR(0)) - 1

UserAppPath = strUserAppPath(1:ILEN)

ILEN = INDEX(strExePath,CHAR(0)) - 1

ExePath = strExePath(1:ILEN)

And much more...

0 Kudos
1 Solution
Wendy_Doerner__Intel
Valued Contributor I
2,598 Views
Charlie,

Thanks for posting your code.

The unbalanced stack error was caused by the incorrect calling convention of the fortran subroutine.The VB code assumes the subroutine cleans up the stack when returning; however, the default calling convention of Fortran requires the caller clean up the stack. Then the runtime system detected the mis-matched stack pointer before/after the call. To fix this, please

Change the subroutine to STDCALL like:

!DEC$ATTRIBUTES DLLEXPORT,STDCALL :: FCCMod

For reference:
http://support.microsoft.com/kb/153586
The Mixed Language section of the IntelVIsual Compiler documentation

------

Wendy

Attaching or including files in a post

View solution in original post

0 Kudos
9 Replies
Wendy_Doerner__Intel
Valued Contributor I
2,598 Views
One change in the Intel Visual Fortran Compiler 2011 is that strings are no longer null terminated (they were by chance in previous releases). Fortran expects this, but Basic does not generate null terminated strings so you may need to add null at the end of strings you are passing to Fortran.

------

Wendy

Attaching or including files in a post

0 Kudos
CharlieG
Beginner
2,598 Views
I tried concatenating a CHR(0) to the end of my strings in VB before passing them to Fortran but it did not fix the error.
0 Kudos
strohhaecker
Beginner
2,598 Views
> strings are no longer null terminated (they were by chance in previous releases). Fortran expects this

Can you please elaborate on this? As I understood, it's the other way round, older versions zero-padded the Fortran strings so passing them to C-style routines wasworking (by accident), but the 12.x release may not "terminate" the Fortran string with a zero character so requires user interaction.
0 Kudos
Steven_L_Intel1
Employee
2,598 Views
There was a case where a user program was relying on a character variable being followed by a NUL, which sometimes was the case in version 11.1 but much less so in version 12. However, I don't think VB uses NUL-terminated strings. Either it passes a BSTRING descriptor (which requires calling routines to create and read in Fortran) or it passes just the data with no length.
0 Kudos
Wendy_Doerner__Intel
Valued Contributor I
2,598 Views
I think we would need a reproducing test case or more detail on the error message to give you any more help. It does look like the error message is trying to mark a disconnect between your call to DLL so I would like at the parameters you are passing to see if they are in format your called procedure is expecting.

------

Wendy

Attaching or including files in a post

0 Kudos
CharlieG
Beginner
2,598 Views
Here's a test case that reproduces the error.

Charlie
0 Kudos
Wendy_Doerner__Intel
Valued Contributor I
2,599 Views
Charlie,

Thanks for posting your code.

The unbalanced stack error was caused by the incorrect calling convention of the fortran subroutine.The VB code assumes the subroutine cleans up the stack when returning; however, the default calling convention of Fortran requires the caller clean up the stack. Then the runtime system detected the mis-matched stack pointer before/after the call. To fix this, please

Change the subroutine to STDCALL like:

!DEC$ATTRIBUTES DLLEXPORT,STDCALL :: FCCMod

For reference:
http://support.microsoft.com/kb/153586
The Mixed Language section of the IntelVIsual Compiler documentation

------

Wendy

Attaching or including files in a post

0 Kudos
CharlieG
Beginner
2,598 Views
That did it. Thanks for the help.
0 Kudos
onkelhotte
New Contributor II
2,598 Views
Had the same problem with WPF and a Fortran DLL.

That didnt happen with Windows Forms and the same DLL though...

Now I will have a nice weekend :-)

Markus
0 Kudos
Reply