C另一个内存/段故障错误

时间:2022-09-06 15:19:15

[EDIT:] I have removed a lot of code now. And I have reduced it to a more straight forward question.

[编辑:]我现在删除了很多代码。我把它缩小为一个更直接的问题。

Is it OK, to pass this variable...

传递这个变量是没关系的......

char Record_Info[file_length + 1];

to another function, like this:

到另一个函数,像这样:

listdir(tempdesc, 0, sockid, Record_Info);

which uses this header:

使用此标头:

int listdir(char *dirname, int lvl, int sockid, char Record_Info[])

And the listdir() can call its-self many times?

listdir()可以多次调用它自己?

.

* Orig Question *

*原始问题*

I was wondering if another set of eyes can look at this please. It was working, but I added some more code (probably more sprintf, near the bottom) and now a Segmentation fault occurs.

我想知道另一组眼睛是否可以看到这个。它工作正常,但我添加了一些代码(可能更多的sprintf,靠近底部),现在发生了分段错误。

This program creates/sends the web page, correctly, but crashes when it returns from the send_recordings_list() function back to the parsing_request() function. I don't think its necessary to understand how its formatting the data from the file, but I included that bit anyway.

该程序正确地创建/发送网页,但是当它从send_recordings_list()函数返回到parsing_request()函数时崩溃。我认为没有必要了解它如何格式化文件中的数据,但无论如何我都包含了这个位。

For the most part, I'm using static variables, and when I attempt to free() any thing, it causes another seg fault, or gcc lib fault. Also I am unsure if passing the variables between the functions() might be causing the issue?

在大多数情况下,我使用静态变量,当我尝试释放()任何东西时,它会导致另一个seg错误或gcc lib错误。另外我不确定在函数()之间传递变量是否可能导致问题?

Also worth mentioning, a different function (not shown in here) sends out a web page which is about 4MB, I can call this one many times and it does not crash. I have placed a few // ****** at some lines which might be of interest?

另外值得一提的是,一个不同的功能(这里没有显示)发出一个大约4MB的网页,我可以多次调用它,它不会崩溃。我在一些可能感兴趣的线路上放了一些// ******吗?

Oh yes, I have not done much standard error handling yet... (I'll do it later), so assume its a spherical chicken in a vacuum.

哦,是的,我还没有做太多标准的错误处理......(我稍后会这样做),所以假设它是一个真空中的球形鸡。

.

So it starts at parsing_request()

所以它从parsing_request()开始

goes to send_recordings_list()

去send_recordings_list()

goes to listdir() . (this calls its self many times)

转到listdir()。 (这称自己很多次)

back to send_recordings_list()

回到send_recordings_list()

. (seg fault here)

。 (这里有段错误)

back to parsing_request()

回到parsing_request()

.

I'm hoping some will go 'ahh i see what you did fool......'

我希望有人会去'啊,我看到你做了什么傻瓜......'

Embedded Linux 2.6.27, GCC 4.2.4, 32MB Ram

嵌入式Linux 2.6.27,GCC 4.2.4,32MB Ram

void parsing_request(int sockid, char *buff)
{
  char *res_line=malloc(MAXLINE), path[MAXLINE], *line;
  // Cut code
  if (strcmp(line, "/recordings_body.htm") == 0)
  {
    send_recordings_list (sockid);      // ****** IT GOES HERE 1st ******
    return;
  }
  free (res_line);
  return;
}




int send_recordings_list(int sockid)
{
    int  hnd;
    int  hnd2;
    char tempdesc[MAX_PATH_LENGTH];


    // Copy all of the data <1 MiB   (Slow reading)
    int file_length = lseek (hnd2, 0, SEEK_END);
    char Record_Info[file_length + 1];      // ******
    lseek (hnd2, 0, SEEK_SET);
    read (hnd2, Record_Info, file_length);
    close (hnd2);

    // Cut out code

    del_file_cnt = 0;
    sprintf (tempdesc, "%s/Recordings", TOPPY_DIR);
    listdir(tempdesc, 0, sockid, Record_Info);        // ***** Major 2nd call here
    return 0;
}





int listdir(char *dirname, int lvl, int sockid, char Record_Info[])
{

    int i;
    DIR* d_fh;
    struct dirent* entry;
    char longest_name[4096];
    char tempdesc[4096], morespace[128];
    int  row_col;
    char chan_name[128], alt_name[128], desc[500], category[128], rec_time[14], start_time[14], end_time[14];
    char trim_file_name[128], trim_file_name2[128], trim_alt_name[128], file_links[1535];
    char logo[128];
    int  length, u_score, dot;
    char *looky_pos1, *looky_pos2;
    int  is_match;

    struct tm tm_rec, tm_start, tm_end;
    time_t tim_rec, tim_start, tim_end;

    // Cut out code

  return 0;
}

3 个解决方案

#1


4  

Sorry, this is a hopeless mess. Learn how to use Linux tools like valgrind and gdb. Check carefully that the memory you request is eventually freed once and after its use is done. Check that you don't pass pointers to local variables out of functions. Check that your strings are long enough to accomodate the data expected (or check as part as your as of now nonexistent error handling).

对不起,这是一个绝望的混乱。了解如何使用valgrind和gdb等Linux工具。仔细检查您请求的内存最终会在使用完毕后释放一次。检查是否没有从函数中传递指向局部变量的指针。检查您的字符串是否足够长以容纳预期的数据(或检查作为您现在不存在的错误处理的部分)。

One debugging strategy that is suprisingly effective is the Teddy Bear Consultant: Get a teddy bear and explain your problem to it step by step. Only if that doesn't help is a real human warranted. [It works because sorting out your understanding to explain it forces you to really think it through.]

一个令人惊讶的有效调试策略是泰迪熊顾问:买一只泰迪熊并逐步解释你的问题。只有这样做无济于事才是真正的人类保证。 [它的工作原理是因为整理出你的理解来解释它会迫使你真正思考它。]

#2


1  

After looking quickly at your code, you have two options:

快速查看代码后,您有两种选择:

  1. Learn how to handle errors, how to manage resources and how to avoid global variables
  2. 了解如何处理错误,如何管理资源以及如何避免全局变量
  3. Use a programming language which does it for you
  4. 使用编程语言为您完成

Both will take a long time but using the second approach, you will be much more productive.

两者都需要很长时间,但使用第二种方法,您将更有效率。

I suggest to try to implement the above with Python just to get a feeling.

我建议尝试用Python实现上面的内容,以获得一种感觉。

#3


0  

Answering your most recent post, asking if you can pass the character value into the function... I don't understand what you're trying to do. In the function prototype you have the final parameter as a char variable_name[], are you trying to pass a array in? If so, why not use a pointer?

回答你最近的帖子,询问你是否可以将字符值传递给函数...我不明白你要做什么。在函数原型中,您将最终参数作为char variable_name [],您是否尝试传入数组?如果是这样,为什么不使用指针?

so the prototype would like like:

所以原型想像:

int listdir(char *dirname, int lvl, int sockid, char* Record_Info);

Passing by reference always tends to give you a little bit more control, whether you want it or not. Are you trying to pass in a specific element of an array?

无论你是否想要,通过引用传递总是会给你更多的控制。您是否尝试传入数组的特定元素?

#1


4  

Sorry, this is a hopeless mess. Learn how to use Linux tools like valgrind and gdb. Check carefully that the memory you request is eventually freed once and after its use is done. Check that you don't pass pointers to local variables out of functions. Check that your strings are long enough to accomodate the data expected (or check as part as your as of now nonexistent error handling).

对不起,这是一个绝望的混乱。了解如何使用valgrind和gdb等Linux工具。仔细检查您请求的内存最终会在使用完毕后释放一次。检查是否没有从函数中传递指向局部变量的指针。检查您的字符串是否足够长以容纳预期的数据(或检查作为您现在不存在的错误处理的部分)。

One debugging strategy that is suprisingly effective is the Teddy Bear Consultant: Get a teddy bear and explain your problem to it step by step. Only if that doesn't help is a real human warranted. [It works because sorting out your understanding to explain it forces you to really think it through.]

一个令人惊讶的有效调试策略是泰迪熊顾问:买一只泰迪熊并逐步解释你的问题。只有这样做无济于事才是真正的人类保证。 [它的工作原理是因为整理出你的理解来解释它会迫使你真正思考它。]

#2


1  

After looking quickly at your code, you have two options:

快速查看代码后,您有两种选择:

  1. Learn how to handle errors, how to manage resources and how to avoid global variables
  2. 了解如何处理错误,如何管理资源以及如何避免全局变量
  3. Use a programming language which does it for you
  4. 使用编程语言为您完成

Both will take a long time but using the second approach, you will be much more productive.

两者都需要很长时间,但使用第二种方法,您将更有效率。

I suggest to try to implement the above with Python just to get a feeling.

我建议尝试用Python实现上面的内容,以获得一种感觉。

#3


0  

Answering your most recent post, asking if you can pass the character value into the function... I don't understand what you're trying to do. In the function prototype you have the final parameter as a char variable_name[], are you trying to pass a array in? If so, why not use a pointer?

回答你最近的帖子,询问你是否可以将字符值传递给函数...我不明白你要做什么。在函数原型中,您将最终参数作为char variable_name [],您是否尝试传入数组?如果是这样,为什么不使用指针?

so the prototype would like like:

所以原型想像:

int listdir(char *dirname, int lvl, int sockid, char* Record_Info);

Passing by reference always tends to give you a little bit more control, whether you want it or not. Are you trying to pass in a specific element of an array?

无论你是否想要,通过引用传递总是会给你更多的控制。您是否尝试传入数组的特定元素?