【问题标题】:Dllimport System.StackOverflowException in WebApp, Console works fineWebApp中的Dllimport System.StackOverflowException,控制台工作正常
【发布时间】:2016-03-15 03:41:58
【问题描述】:

我很困惑,已经搜索了很长一段时间。 我正在从 c# 调用一个 c++ DLL 我可以在一个简单的控制台应用程序中使其一切正常,但相同的代码在 WebApp 中不起作用

我正在使用 Windows 10 64 位,但我已强制一切使用 32 位,并且调用是 Cdecl

c#代码:

namespace WebApp
{
  public class LibWrap
  {
    [DllImport("mdll.dll", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
    public static extern Int32 MySize(Int64 start,
      Int32 count,
      Int64[] from,
      Int64[] to,
      Int32[] ids,
      ref Int32 id_cnt);
  }

    public partial class Default : System.Web.UI.Page
    {
        protected void Button1_Click(object sender, EventArgs e)
        {
          Int64 l_start = 1431644400;
          Int64[] l_from = {
                                1431644400,
                                1432114200,
                                1432162800,
                                1432278000,
                                1432335600
                            };
          Int64[] l_to = {
                                1431673200,
                                1432119600,
                                1432200600,
                                1432288800,
                                1432364400
                            };
          Int32[] l_IDs = {
                                1,
                                1,
                                1,
                                1,
                                1
                            };
          int l_idCnt = 0;
          Int32 l_Count = LibWrap.MySize(l_start, l_from.Length, l_from, l_to, l_IDs, ref l_idCnt);
        }
    }
}

被调用的DLL头代码是:

#define DEF_EXPORT   __declspec(dllexport) __cdecl

int32_t DEF_EXPORT MySize(int64_t   start,
                          int32_t   count,
                          int64_t * from,
                          int64_t * to,
                          int32_t * ids,
                          int32_t & id_cnt);

c++源码:

int32_t DEF_EXPORT MySize(int64_t   start,
                          int32_t   count,
                          int64_t * from,
                          int64_t * to,
                          int32_t * ids,
                          int32_t & id_cnt)
{
  MyCore mycore;

  int32_t sz = 0;

  if (count)
  {
    mycore.Count = count;

    mycore.Ids    = ids;
    mycore.From   = from;
    mycore.To     = to;

    mycore.Start = start;

    IDList id_positions[MY_LIMIT];
    id_cnt = 1;

    sz = mycore.OutputSize(id_positions, id_cnt);

  }
  return sz;
}

正如我所说,WebApp 在尝试调用 MySize 时会引发 System.StackOverflowException 错误。 一旦我进入MySize 函数(即在第一行运行之前),使用本机代码调试System.StackOverflowException 感谢任何见解,我在网上找到的每个解决方案似乎都已包含在我的案例中。

【问题讨论】:

  • 出于好奇,如果您将MySize 方法存根(直接返回0)会发生什么?
  • @Rob 值得一试。
  • 您的 C++ 代码中是否存在任何可疑的递归调用?
  • 我打赌 webapp 正在寻找旧的 dll
  • @Rob 感谢您的想法,但立即返回会产生相同的错误。

标签: c# c++ .net dll dllimport


【解决方案1】:

好的,这是提供给控制台应用程序与 Web 应用程序的堆栈内存问题 在 32bit 下,Web 应用程序只能获得 256Kb。

罪魁祸首是数组:

IDList id_positions[MY_LIMIT];

将其移至动态分配使其脱离堆栈并解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    相关资源
    最近更新 更多