有问题的代码无法修复它。 [英] Problematic code can't fix it.

查看:70
本文介绍了有问题的代码无法修复它。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个很长的代码,所以不要试图理解它我把它放在以防万一

This is a long code so don't try to understand it I put it just in case

static void BreakSingleGem(Gems gem)
        {
            if (output.Text != "")
            {
                if (gem is Ruby || gem is Garnet)
                {
                    if (!HasRedHearts(allUpgrades))
                        allUpgrades.Add(new RedHearts());
                }
                if (gem is Sapphire || gem is Emerald)
                {
                    if (!HasBlueHearts(allUpgrades))
                        allUpgrades.Add(new BlueHearts());
                    if (!HasGreenHearts(allUpgrades))
                        allUpgrades.Add(new GreenHearts());
                }

                foreach (Items items in allUpgrades)
                {
                    if (items.GetType() == gem.GetType() && items.Amount > 0)
                    {
                        if (items is Garnet || items is Ruby)
                        {
                            allUpgrades[0].Amount += ((Gems)items).GetRedCrystals.Amount;
                            allUpgrades[allUpgrades.Count - 1].Amount +=
                                ((Gems)items).GetRedHearts.Amount;
                            InsertRefines((Gems)items);

                            items.Amount--;
                            if (items is Garnet)
                            {
                                if (((Gems)items).GetGrade != 1)
                                    allUpgrades[((Gems)items).GetGrade * 2 - 2].Amount += 2;
                            }
                            if (items is Ruby)
                            {
                                if (((Gems)items).GetGrade != 1)
                                    allUpgrades[((Gems)items).GetGrade * 2 - 3].Amount += 2;
                            }
                            break;
                        }
                        if (items is Sapphire || items is Emerald || items is BlackOnyx)
                        {
                            if (items is Sapphire || items is BlackOnyx)
                            {
                                allUpgrades[0].Amount += ((Gems)items).GetBlueCrystals.Amount;
                                if (items is Sapphire)
                                    allUpgrades[allUpgrades.Count - 2].Amount +=
                                        ((Gems)items).GetBlueHearts.Amount;
                            }
                            if (items is Emerald || items is BlackOnyx)
                            {
                                allUpgrades[1].Amount += ((Gems)items).GetGreenCrystals.Amount;
                                if (items is Emerald)
                                    allUpgrades[allUpgrades.Count - 1].Amount +=
                                        ((Gems)items).GetGreenHearts.Amount;
                            }
                            InsertRefines((Gems)items);

                            items.Amount--;
                            if (items is Emerald || items is BlackOnyx)
                            {
                                if (((Gems)items).GetGrade != 1)
                                    allUpgrades[((Gems)items).GetGrade * 2 - 1].Amount += 2;
                            }

                            if (items is Sapphire || items is BlackOnyx)
                            {
                                if (((Gems)items).GetGrade != 1)
                                    allUpgrades[((Gems)items).GetGrade * 2 - 2].Amount += 2;
                            }
                            break;
                        }
                    }
                }
                int totalPrice = CalculateNewPrice();
                InsertToOutput(allUpgrades, totalPrice);
            }
        }





这段代码带有一个宝石,并从它的组件中分解出来。我无法修复的非常奇怪的部分如下:

当我正常运行代码时,由于某些我无法理解的原因,这个过程会重复两次。但是当我在这段代码中的任何地方放置一个制动点,迫使它在执行它时暂停一次并强制我手动恢复它,代码只执行一次就好了!我不知道如何解决这个问题...更改代码不起作用,因为如果我插入一个随机制动点它会做它应该做的事情,但是如果我不重复它会重复两次......

请帮帮我



This code takes a gem and breaks it from it's components. The very weird part about it that I can't fix is the following:
When I run the code normally this process is repeated twice for some reason I can't understand. But when I put a brakepoint anywhere inside this code forcing it to pause once while doing it and forcing me to manually resume it, the code is only performed once like it should! I have no idea how to fix this... Changing the code doesn't work because it does what it should if I insert a random brakepoint but it repeats twice if I don't...
Please help me

推荐答案

根据你的描述,这个方法是受害者,所以代码与给定的问题无关(除非在这里有一些间接的递归)代码 - 但是它可能总是会使用相同的输入数据重复两次。)



搜索上述方法的所有*调用者*。是否通过某些GUI回调调用?如果在某些断点处停止,这可能会干扰控制流程。



您也可以编写一个stackdump作为此方法中的第一个操作,确定谁在正常执行时调用它(即在调试器外执行。)



例如请参阅如何在.NET中记录当前的调用堆栈 [ ^ ]。



干杯

Andi
This method is the "victim" according to your description, so the code is irrelevant for the given problem (unless there was some indirect recursion in this code - but then it presumably would always repeat twice with the same input data).

Search for all *callers* of the above mentioned method. Is it called by some GUI callback? This might disturb control flow if stopping on some breakpoints.

You may also write a stackdump as first action in this method do identify who calls it at normal execution (i.e. when executed outside the debugger).

E.g. see How to log the current call stack in .NET[^] on how to get the stack dump.

Cheers
Andi

除了解决方案1和你自己的调试工作描述。



再做一个简单的步骤:当你的执行在断点处停止时描述,打开另一个名为调用堆栈的调试窗口。它会显示来电的来源,直到最上面的步骤。当它再次停止时,堆栈可能会有所不同,然后比较这些情况,并在调用来自您没想到或计划的某种方法时找出案例。



-SA
In addition to Solution 1 and to your own description of your debugging effort.

Do one additional simple step: when your execution stops at the break point you described, open another debug window called "Call stack". It will show you where the call came from, up to the very top of the step. When it stops again, the stack can be different, and then compare those cases and find out the case when the call came from some method you did not expect or plan for.

—SA


这篇关于有问题的代码无法修复它。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆