V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
BBrother
V2EX  ›  问与答

分享一个 C#程序的神秘 bug,顺便问问为什么会这样?

  •  
  •   BBrother · 4 天前 · 401 次点击

    在以下代码中,如果注释掉UIDocument UIDocument = new UIDocument(Document);这一行代码(倒数第二行),运行后可以正常输出到init finish,但是这行代码如果存在则只能输出到$"2, {clientId.GetGUID()}"这一行,然后会报运行时错误:

    未经处理的异常:  System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
    

    为什么会存在这种后面还没运行到的代码可以影响前面代码的报错?

     [STAThread]
            static void Main(string[] args)
            {
                // 初始化
                Init();
                Product revitInstance = Product.GetInstalledProduct();
                Console.WriteLine("1");
                var clientId = new ClientApplicationId(Guid.NewGuid(), "testing", "ADSK.Username");
                Console.WriteLine($"2, {clientId.GetGUID()}");
                revitInstance.Init(clientId, "I am authorized by Autodesk to use this UI-less functionality.");
                Console.WriteLine("3");
                Autodesk.Revit.ApplicationServices.Application Application = revitInstance.Application;
                Console.WriteLine("4");
                Document Document = Application.OpenDocumentFile(inputPath);
                Console.WriteLine("5");
                UIDocument UIDocument = new UIDocument(Document);
                Console.WriteLine("init finish");
                
                ......
            }
    
    7 条回复    2024-06-25 13:11:46 +08:00
    idealhs
        1
    idealhs  
       4 天前
    你这是 Unity 吧?先说清楚环境,.NET 还是 mono 啥的
    BBrother
        2
    BBrother  
    OP
       4 天前
    @idealhs #1 .net framework 4.8 控制台应用程序,引用了 revit 的 dll
    idealhs
        3
    idealhs  
       4 天前
    @BBrother 单步调试吧,或者你把全部代码发出来看看,你这 Init(); 就不知道干啥了,跑不起来
    BBrother
        4
    BBrother  
    OP
       4 天前
    @idealhs #3 init 可以注释掉不影响这个现象的发生,单步不了,环境比较特殊,需要在一台没有 vs 的机器上运行。
    Eiden
        5
    Eiden  
       4 天前
    vs 可以远程调试的
    a33291
        6
    a33291  
       4 天前
    @BBrother 先确认在开发机是否正常,然后用依赖检查工具检查在目标机器上的 revit dll 的依赖是否满足,特别是运行时环境(应该是 vc++)
    idealhs
        7
    idealhs  
       4 天前
    @BBrother #4 没法开发机调的话,上 Visual Studio remote debugger
    https://learn.microsoft.com/en-us/visualstudio/debugger/remote-debugging?view=vs-2022
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2314 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 11:12 · PVG 19:12 · LAX 04:12 · JFK 07:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.