记录一个i变量引发的事故

概述

近期开发中遇到一个特别的问题,觉得很有必要与你下来。就是由于在开发中一个很小的疏忽,导致了很大的问题,是什么呢?

现象

我的程序突然引发了v8内部的错误,提示都是c++的,如下。程序一启动就直接崩溃。没有任何错误提示。

#
# Fatal error in , line 0
# Fatal JavaScript invalid size error 195652814
#
#
#
#FailureMessage Object: 0x7ffd9ac17dd0
 1: 0xb76401  [node]
 2: 0x1c09824 V8_Fatal(char const*, ...) [node]
 3: 0xe7dd5e  [node]
 4: 0xff5168  [node]
 5: 0x1007649  [node]
 6: 0x11ed9f3 v8::internal::Runtime_GrowArrayElements(int, unsigned long*, v8::internal::Isolate*) [node]
 7: 0x15f20b9  [node]
Trace/breakpoint trap

如果放在平时,估计直接崩溃了,重装系统都有可能。比较幸运的是,在出现此错误和正常运行时,我只改了少量的代码。我觉得问题可能是出现这些代码上,就仔细比对了这些代码。结果还真发现了问题。

      for (let i = 0; i < res.data.items.length; i++) {
            const info = objectHelper.cloneDeepJsonParse(res.data.items[i])

            if (info.property.detailModulOfFree == undefined) info.property.detailModulOfFree = []
            for (let j = 0; j < info.property.detailModulOfFree.length; j++)
                info.property.detailModulOfFree[j] = parseInt(info.property.detailModulOfFree[j])

            if (info.property.detailModulOfPromote == undefined) info.property.detailModulOfPromote = []
            for (let j = 0; j < info.property.detailModulOfPromote.length; j++)
                info.property.detailModulOfPromote[j] = parseInt(info.property.detailModulOfPromote[j])

            const topic: any = {
                id: info.id,
            }

            for (let j = 0; j < info.children.length; j++) {
                const child = info.children[j]
                child.property.topicType = child.property.topicType == undefined ? 1 : child.property.topicType

                if (child.property.detailModulOfFree == undefined) child.property.detailModulOfFree = []
                for (let k = 0; k < child.property.detailModulOfFree.length; k++)
                    child.property.detailModulOfFree[k] = parseInt(child.property.detailModulOfFree[k])

                if (child.property.detailModulOfPromote == undefined) child.property.detailModulOfPromote = []
                for (let k = 0; k < child.property.detailModulOfPromote.length; k++)
                    child.property.detailModulOfPromote[k] = parseInt(child.property.detailModulOfPromote[k])

                if (child.property.detailModulOfVip1 == undefined) child.property.detailModulOfVip1 = []
                for (let k = 0; i < child.property.detailModulOfVip1.length; k++)
                    child.property.detailModulOfVip1[k] = parseInt(child.property.detailModulOfVip1[k])

                if (child.property.detailModulOfVip2 == undefined) child.property.detailModulOfVip2 = []
                for (let k = 0; k < child.property.detailModulOfVip2.length; k++)
                    child.property.detailModulOfVip2[k] = parseInt(child.property.detailModulOfVip2[k])

上面是部分代码,大家能发现问题吗?
我公布一下答案

image

这里谅是K的,错误的写成了i,这个i是外层的一个for循环变量。

总结

  1. 写代码一定要认真,相信没有最好,只有更好。
  2. 尽量不要使用for,建议使用foreach。减少i,j,k这样的变量使用。降低错误的风险。
posted on 2022-08-17 15:39  一级码农VIP  阅读(128)  评论(0编辑  收藏  举报