簡體   English   中英

我單步執行程序時只會得到Stackoverflow異常

[英]I Only get a Stackoverflow exception when stepping through program

該程序在運行時運行良好,但是當我嘗試逐步執行時,得到:

“未知模塊中發生了'System.StackOverflowException類型的未處理的異常。”

這是代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace PInvokeTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int session_handle = 0;
            int flag = 0;
            int didsetup = 0;
            int defPort = 0;
            int i = 0, j = -1;
            short[] ROM;
            ROM = new short[9];
            short type_test = 0;
            short port_num = 0, port_type = 1;
            byte[] state_buf = new byte[5125];
            StringBuilder ID_buf = new StringBuilder();
            StringBuilder serial = new StringBuilder();
            StringBuilder serialtmp = new StringBuilder();



            //Finds default device type and port
            defPort = TMReadDefaultPort(out port_num, out port_type);

            // get the TMEX driver version
            Get_Version(ID_buf); // STACKOVERFLOW EXCEPTION HERE

            ...

            Console.ReadKey();
        }

        [DllImport("IBFS32.dll")]
        public static extern int TMExtendedStartSession(
            short PortNum,
            short PortType,
            IntPtr EnhancedOptions
        );

        [DllImport("IBFS32.dll")]
        public static extern short TMReadDefaultPort(
            out short port_num,
            out short port_type
        );

        [DllImport("IBFS32.dll")]
        public static extern short Get_Version(
            [MarshalAs(UnmanagedType.LPStr)]StringBuilder ID_buf
        );

        [DllImport("IBFS32.dll")]
        public static extern short TMGetTypeVersion(
            short port_type,
            [MarshalAs(UnmanagedType.LPStr)]StringBuilder ID_buf
        );

        [DllImport("IBFS32.dll")]
        public static extern short TMSetup(
            int session_handle
        );

        [DllImport("IBFS32.dll")]
        public static extern short TMNext(
            int session_handle,
            byte[] state_buf
        );

        [DllImport("IBFS32.dll")]
        public static extern short TMRom(
            int session_handle,
            byte[] state_buf,
            short[] ROM
        );

        [DllImport("IBFS32.dll")]
        public static extern short TMEndSession(
            int session_handle
        );
    }
}

是什么導致它僅在運行時不步進就可以工作? 我有另一個程序使用的代碼非常相似(dll函數位於不同的類中),但是無論如何運行,它都會出現stackoverflow異常。

編輯:

當我使用該dll的64位版本並在x64中構建程序時,它一直都可以正常工作...我不知道該信息對您是否有幫助。

我需要運行32位版本。

您必須為StringBuilder指定初始容量(通過構造函數),以確保其足夠大以存儲結果。 您正在調用默認構造函數。

您可以改為傳遞System.Text.StringBuilder對象; 封送程序會將指針傳遞給可以操縱的非托管函數。 唯一的警告是,必須為StringBuilder分配足夠的空間用於返回值,否則文本將溢出,從而導致P / Invoke引發異常。

P /調用簡介

StringBuilder ID_buf = new StringBuilder(MaxVersionLength);

是什么導致它僅在運行時不步進就可以工作?

最有可能在Release中運行代碼,但在Debug中過渡,在這些情況下,由於在Debug模式下需要更多信息,因此可用堆棧大小會減少,因此用於執行具體代碼的堆棧上的可用內存會變小,從而產生堆棧溢出異常,由於堆棧已滿。

順便說一下,您有一個stackoverflow危險異常,因此最好對其進行修復,直到在Release和生產環境中遇到相同的問題為止。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM