首页 > 解决方案 > stm32:20次后添加新节点到链表,然后系统崩溃了

问题描述

编辑:2019/1/22(我通过将堆大小从 512 字节增加到 2048 字节来解决这个问题)

[首先对不起我的英语不好!]

我有一个链表,有时我会在其中添加一些新节点,它在 Visual Studio(Visual C++ 编译器)上运行良好,但使用 ARM Compiler V5 时出现问题。

我的代码在 stm32f103vet6 上运行,20 次后将新节点添加到链接列表然后系统崩溃,电脑跳转到 HardFault_Handler。我不知道为什么?我的代码在窗口(视觉工作室)上工作正常。

我已经google了几天,但没有理想

// 这是我的代码

typedef struct OutputLineStructure   
{

    int16_t OP_Name;
    uint16_t Time1;
    uint16_t Value;
    uint16_t Time2;
    struct  OutputLineStructure *pNext;
    struct  OutputLineStructure *pPrev;

} OutputLine;
//

typedef struct StoryboardStruct   
{
    OutputLine *Line;
    uint16_t Lenght;
    uint16_t ID;
} StoryboardStructure;
//

StoryboardStructure _Storyboard[12]; 

// Set break point inside to view exeption
char SE_File[100] = {[0 ... 99]  = 0};
char SE_Function[100] = {[0 ... 99] = 0};
char SE_Comment[150] = {[0 ... 99] = 0};
void SystemFault_Exception(char* File, char* Function, char* Comment, uint32_t ErrorCode)
{
    sprintf(SE_File, "%s", File);

    sprintf(SE_Function, "%s", Function);

    sprintf(SE_Comment, "%s", Comment); 

    uint32_t Code = ErrorCode;

    delayMs(10); // Set break point here 
}
//

void StoryboardInit(StoryboardStructure *MyStoryboard, uint16_t ID)
{
    MyStoryboard->Lenght = 0;
    MyStoryboard->Line = NULL;
    MyStoryboard->ID = ID;
}
//

void StoryboardAllInit(void)
{
    for (int i = 0 ; i< 12; i++)
    {
        StoryboardInit(_Storyboard+i, i+1);
    }
}
//

void StoryboardAddLine(StoryboardStructure *MyStoryboard, int16_t _Name, uint16_t _Time1, uint16_t _Value, uint16_t _Time2)
{
    if (MyStoryboard->Lenght == 0)
    {
        if (MyStoryboard->Line != NULL) 
        {
            SystemFault_Exception("storyboard","StoryboardAddLine","HeadLine is not NULL",0);
            while(1); // the pc never jump to this point
        }
        OutputLine *NewLine = (OutputLine*)malloc(sizeof(OutputLine));

        NewLine->OP_Name = _Name;
        NewLine->pNext = NULL;
        NewLine->pPrev = NULL; 

        NewLine->Time1 = _Time1; //Set break point and error occur here
        NewLine->Time2 = _Time2; //Set break point and error occur here
        NewLine->Value = _Value; //Set break point and error occur here

        MyStoryboard->Line = NewLine;
        MyStoryboard->Lenght ++;
    }
    else
    {
        OutputLine *NewNode = (OutputLine*)malloc(sizeof(OutputLine)); 
        NewNode->OP_Name = _Name;
        NewNode->Time1 = _Time1;
        NewNode->Time2 = _Time2;
        NewNode->Value = _Value;


        OutputLine *Scan = MyStoryboard->Line;
        while (Scan->pNext != NULL)
        {
            Scan = Scan->pNext;
        }

        Scan->pNext = NewNode;
        NewNode->pPrev = Scan;
        NewNode->pNext = NULL;
        MyStoryboard->Lenght++;
    }
}
//

int main(void )
{
    StoryboardAllInit();

    for (int id = 1; id< 10; id++)
    {
        for (int line = 0; line < 10; line ++)
        StoryboardAddLine(_Storyboard + id-1,id + line,0,0,0);
          // When ID = 3 and line  = 0. system has crashed
    }

    while(1)
    {

    }
}
//

见 main 函数,for 循环,当 (id == 3 and line == 0) 时系统崩溃。请参阅 StoryboardAddLine 函数,块(MyStoryboard->Lenght == 0),当我为 MyStoryboard 设置属性时发生错误,例如:NewLine->Time1 = _Time1;

感谢您的帮助 !

标签: stm32allocation

解决方案


推荐阅读