简体   繁体   English

pInvokeStackImbalance MDA警告以及如何关闭或修复它

[英]pInvokeStackImbalance MDA Warning and how to turn it off or fix it

    [DllImport( "zlib32" )]
    private static extern ZLibError compress2( 
                byte[] dest,
                ref int destLength, 
                byte[] source, 
                int sourceLength, 
                ZLibQuality quality 
            );

every time I call this I get an MDA warning telling me the stack is imbalanced, which is a nightmare for debugging. 每次我打电话给我都会收到MDA警告告诉我堆栈是不平衡的,这是调试的噩梦。 I want to either turn this warning off, or fix the issue 我想要关闭此警告,或者解决问题

This MDA raised to tell you that you have a problem with parameters type you are using for PInvoke call. 此MDA提出告诉您,您在PInvoke调用中使用的参数类型存在问题。 Generally, it is very bad idea to turn it off since it warns about problem in your code and imbalanced stack leads to errors (sometimes hard to find) in future. 一般来说, 关闭它是非常糟糕的,因为它警告你的代码中的问题和不平衡的堆栈导致将来出现错误(有时很难找到)。

Usually, common mistake is selected matching for unmanaged type with managed one. 通常,选择与托管类型的非托管类型匹配的常见错误。

In your case, original defininition (I take a look to zlib125.zip ): 在你的情况下,原始的定义(我看看zlib125.zip ):

ZEXTERN int ZEXPORT compress2 OF((Bytef *dest,   uLongf *destLen,
                                  const Bytef *source, uLong sourceLen,
                                  int level)); 

which can be traslated to if library was compiled with 64-bit support for unsigned long : 如果库是使用64位支持unsigned long编译的,那么可以进行转换:

static int compress2(
    byte[] dest,
    ref ulong destLength,
    byte[] source,
    ulong sourceLength,
    int level)

Ensure ZLibQuality enumeration is based on int. 确保ZLibQuality枚举基于int。 Probably, your error is usage int instead of ulong for both lengths. 可能是,对于两个长度,您的错误是使用int而不是ulong

As stated David Heffernan there are plenty other reasons to fail to find exact one give us link to library actually used for development if you still want to know. 正如David Heffernan所述,还有很多其他原因未能找到确切的一个原因,如果您仍然想知道,请将我们链接到实际用于开发的库。

  • traditional compilation original library with Visual C++ will result you get library with 32-bit support only, so original definition you provided in question is valid unless ZLibQuality enumeration is not int based 使用Visual C ++的传统编译原始库将导致您只获得32位支持的库,因此您提供的原始定义是有效的,除非ZLibQuality枚举不是基于int

  • maybe you try to use library compiled for other calling convention such as cdecl instead of stdcall 也许你试图使用编译为其他调用约定的库,如cdecl而不是stdcall

  • maybe you try to use modified library where compress2 function takes additional parameters. 也许你尝试使用修改后的库,其中compress2函数需要额外的参数。

We can find whats wrong when we can see exact library you are using. 当我们看到您正在使用的精确库时,我们可以找到错误的内容。

long or unsigned long usually 32-bit under Windows and mapped to int or uint respectively. longunsigned long 通常在Windows下为32位 ,并分别映射到intuint Since you have troubles with original declaration I assumed that maybe you are using specific library with 64-bit support. 由于你有原始声明的麻烦我假设你可能正在使用64位支持的特定库。 Thanks to David Heffernan to point me makes my notice clearly. 感谢David Heffernan指出我清楚地通知我。

You can use folowing resourses as reference: 您可以使用以下资源作为参考:

/Offtopic: /无关:

Why do you use you own implementation with self bindings to library? 为什么使用自己的实现与库的自绑定? You can use: 您可以使用:

  • DotNetZip - Zip and Unzip in C#, VB, any .NET language - DotNetZip is an easy-to-use, FAST, FREE class library and toolset for manipulating zip files or folders. DotNetZip - 在C#,VB,任何.NET语言中压缩和解压缩 - DotNetZip是一个易于使用,快速,免费的类库和工具集,用于处理zip文件或文件夹。 Zip and Unzip is easy: with DotNetZip, .NET applications written in VB, C# - any .NET language - can easily create, read, extract, or update zip files. Zip和Unzip很简单:使用DotNetZip,用VB编写的.NET应用程序,C#(任何.NET语言)都可以轻松创建,读取,提取或更新zip文件。 For Mono or MS .NET. 对于Mono或MS .NET。

  • or ready to use 7-zip bindings: SevenZipSharp - Managed 7-zip library written in C# that provides data (self-)extraction and compression (all 7-zip formats are supported). 或准备使用7-zip绑定: SevenZipSharp - 用C#编写的托管7-zip库,提供数据(自我)提取和压缩(支持所有7-zip格式)。 It wraps 7z.dll or any compatible one and makes use of LZMA SDK. 它包装7z.dll或任何兼容的,并使用LZMA SDK。

The stack imbalance is because you have mis-matching calling conventions or mis-matching function declarations. 堆栈不平衡是因为您具有不匹配的调用约定或错误匹配的函数声明。 I'd be very surprised if zlib32 was using stdcall calling convention. 如果zlib32使用stdcall调用约定,我会非常惊讶。 Surely that uses cdecl . 肯定是使用cdecl I'd want to see your C++ declaration of that function before giving firmer advice. 在给出更坚定的建议之前,我想看看你的C ++声明。

Leave the warning on because it's finding bugs in your code, and fix the mis-matches, whatever they are. 保持警告,因为它在您的代码中发现错误,并修复错误匹配,无论它们是什么。

There could be a real issue here, but I usually have to disable all Managed Debugging Assistants every now and then, since some of these magically get enabled. 这里可能存在一个真正的问题,但我通常必须时不时地禁用所有托管调试助手,因为其中一些神奇地启用了。 Be sure to check Debug | 一定要检查Debug | Exceptions node, then expand the Managed Debugging Assistants and make sure every one of these is disabled. 例外节点,然后展开托管调试助手并确保禁用每个托管调试助手。

EDIT: You will have better luck replacing the P/Invoke with a C++/CLI wrapper that you create for compress2 . 编辑:您将有更好的运气替换P / Invoke与您为compress2创建的C ++ / CLI包装compress2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM