简体   繁体   English

UNIX:recv出现内存问题-malloc.c:3096:sYSMALLOc:断言

[英]UNIX: Memory issue with recv - malloc.c:3096: sYSMALLOc: Assertion

like in the topic I have a problem with my project for university. 就像在主题中一样,我的大学项目有问题。 I've tried to do it myself for a long time now, but with my full-time work my free time is just not enough. 我已经尝试自己做很长时间了,但是通过我的全职工作,我的业余时间还不够。

Error I get: 错误我得到:

all: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || 全部:malloc.c:3096:sYSMALLOc:声明`(old_top ==((((mbinptr)((((char *)&((av)-> bins [((1)-1)* 2])))-__builtin_offsetof (struct malloc_chunk,fd))))&& old_size == 0)|| ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed. (((unsigned long)(old_size)> =(unsigned long)((((((__ builtin_offsetof(struct malloc_chunk,fd_nextsize))+((2 *(sizeof(size_t)))-1))&〜((2 *(sizeof (size_t)))-1)))&&(((old_top)-> size&0x1)&&((unsigned long)old_end&pagemask)== 0)'失败。 Aborted 中止

Faulty part of code 错误的代码部分

void obslugagracza(int sock, int numergracza){
    int wybor, read_size, kto, ile;
    char client_message[1000];
    char message[1000];
    time_t rawtime;
    while(wyscig==0)
    {
        wypisz("Co chcesz zrobic?\n\n1 - Sprawdz ilosc kredytow\n2 - Dodaj kredytow\n3 - Wyplac kredyty\n",sock);
        wypisz("4 - Kto w nastepnym wyscigu?\n5 - Wyniki poprzedniego wyscigu\n6 - Obstaw najblizszy wyscig\n7 - Kiedy wyscig?\n\n", sock);
        while( (read_size = recv(sock , client_message , 100 , 0)) > 0 )
        {
            if(wyscig==1)
                break;
            wybor=atoi(client_message);
            if(wybor==1)
            {
                sprintf(message,"TEMP %i  \n",plist[numergracza].money);
                wypisz(message,sock);
                sleep(4);
            }
            else if(wybor==2)
            {
                wypisz("TEMP", sock);
                read_size = recv(sock , message , maxnamel , 0);
                message[read_size-1]=0;
                plist[numergracza].money+=atoi(message);
                sprintf(message,"TEMP",plist[numergracza].money);
                wypisz(message,sock);
                sleep(4);
            }
            else if(wybor==3)
            {
                wypisz("TEMP", sock);
                read_size = recv(sock , message , maxnamel , 0);
                message[read_size-1]=0;
                if(atoi(message)>plist[numergracza].money)
                    wypisz("TEMP", sock);
                else
                {
                    plist[numergracza].money-=atoi(message);

                    sprintf(message,"TEMP",plist[numergracza].money);
                    wypisz(message,sock);
                }
            }
            else if(wybor==4)
            {
                wypisznastepnych(sock);

            }
            else if(wybor==5)
            {
                if(firstrace==0)
                    wypisz("TEMP", sock);
                else
                {
                    char messaget[100];
                    int i, horsen=8;
                    if (hnumber<horsen)
                        horsen=hnumber;
                    wypisz("TEMP", sock);
                    for(i=0;i<horsen;i++)
                    {
                        if(results[i].inrace==1)
                            sprintf(messaget,"TEMP %s \n", results[i].name);
                        wypisz(messaget,sock);
                    }
                }
            }
            else if(wybor==6)
            {
                wypisznastepnych(sock);
                sleep(3);
                wypisz("TEMP", sock);
                read_size = recv(sock , client_message , maxnamel , 0);
                client_message[read_size-1]=0;
                kto=atoi(client_message);
                wypisz("TEMP", sock);
                read_size = recv(sock , client_message , maxnamel , 0);
                client_message[read_size-1]=0;
                ile=atoi(client_message);
                if(ile>plist[numergracza].money)
                    wypisz("TEMP", sock);
                else
                {
                    plist[numergracza].money-=ile;
                    plist[numergracza].bet+=ile;
                    plist[numergracza].bethorse=kto;
                    sprintf(message,"TEMP",race[kto], ile);
                    wypisz(message,sock);
                }
            }
            else if(wybor==7)
            {
                time( &rawtime);
                sprintf(message,"TEMP",(int)(nextr-rawtime)/60, (int)(nextr-rawtime)%60);
                wypisz(message,sock);
            }
            else
            {
                wypisz("TEMP",sock);
            }
            sleep(3);
            wypisz("Co chcesz zrobic?\n\n1 - Sprawdz ilosc kredytow\n2 - Dodaj kredytow\n3 - Wyplac kredyty\n",sock);
            wypisz("4 - Kto w nastepnym wyscigu?\n5 - Wyniki poprzedniego wyscigu\n6 - Obstaw najblizszy wyscig\n7 - Kiedy wyscig?\n\n", sock);
        }
    }   
}

If I understand correctly, error happens on the second loop of while (client has picked one of options, it works correctly, then menu shows up again, he picks again(doesn'tmatter which option), and error happens. 如果我理解正确,则错误会在while的第二个循环中发生(客户端选择了一个选项,它可以正常工作,然后再次显示菜单,他再次选择了(不选择哪个选项),并且发生了错误。

so error on line 所以在线错误

while( (read_size = recv(sock , client_message , 100 , 0)) > 0 )

What may be the source of this issue? 此问题的根源可能是什么? on few gorums I've read about memory corruption, but I have no idea how to resolve this. 在少数gorum上,我已经读到了有关内存损坏的信息,但是我不知道如何解决此问题。

And small question on the side - is this the proper way to allocate memory? 侧面还有一个小问题-这是分配内存的正确方法吗?

char *messagew ;
messagew=malloc(sizeof(char)*100+1);

I know code quality sucks, but due to my job and 2nd univeristy, I do this program in parts, often not really remembering where I've stopped and what now. 我知道代码质量糟透了,但是由于我的工作和第二届大学,我分部分地执行了此程序,常常无法真正记住我停在哪里以及现在做什么。 Since program is mostly done, I probably will have more questions after I resolve this one (fixing anything now will probably produce more problems with the amount and quality of code). 由于程序大部分都已完成,因此在解决这一问题后,我可能会遇到更多问题(现在修复任何问题都可能在代码的数量和质量方面产生更多的问题)。

Of course all these TEMP messages are only here, to make code shorter - I have normal messages to client, but I doubt they help anything with the problem. 当然,所有这些TEMP消息都只是在这里,以使代码更短-我向客户端发送了正常消息,但我怀疑它们是否对解决问题有帮助。

The source is a memory corruption, no doubt arising as a result of some undefined behaviour in your code, probably a buffer overrun . 来源是内存损坏,这无疑是由于代码中某些未定义的行为 (可能是缓冲区溢出)引起的 The nature of memory bugs is such that the root cause might be far removed from the point where you are getting the error (to be honest, you are quite lucky to be getting a sensible error message at all). 内存错误的本质是,根本原因可能与您得到错误的地方相去甚远(老实说,您很幸运能收到一条明智的错误消息)。

The easiest way to debug this is probably by running your program through Valgrind . 调试此问题的最简单方法可能是通过Valgrind运行程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM