Thread: glibc: corrupted double-linked list

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    2

    glibc: corrupted double-linked list

    Hi!! After some times lurking this is my first question...
    I'm working on a function that should remove any tag from an HTML page and split all the remaining text into an array.
    Here is the code:

    Code:
    char **html::getPageKeywords()
    {
           Pattern * p = Pattern::compile("<body[^>]*?>(.+?)</body[^>]*?>");
       
           Matcher * m = p->createMatcher(getPage());
    
            char * c = getPage();//&v[0];
    
    	std::string temp;
    	temp=p->replace("<script[^>]*?>(.+?)</script[^>]*?>",c,"");
    	temp=p->replace("<[^>]*?>",temp,"");
    	temp=p->replace("</[^>]*?>",temp,"");
    	temp=p->replace("<[^>]*?/>",temp,"");
    
    	std::vector<char> z(temp.length() + 1);
    	std::strcpy(&z[0], temp.c_str());
    	c = &z[0];
    
    	char *out = new char[(signed)strlen(c)+1]; 
            UnToken(c,tokens,out);  // removes characters like @ # $ % ^ & * ; : ' . and so on...
     
           char *no2spaces = new char[(signed)strlen(out)+1];
    	no2spaces=out;
    	OnlyOneSpace(no2spaces);  // removes doubled spaces
    	
    	char *pch = new char[(signed)strlen(out)];
    	pch=strtok(no2spaces," ");
    
    	char **keywords=new char*[(signed)strlen(out)];
    
    	int x=0;
    	while (pch!=NULL)
    	{
    		char * chiave=new char[strlen(pch)+1];
    		chiave=pch;		
    
    		if (chiave[0] != ' ' && strlen(chiave) != 0)
    			keywords[x]=chiave;
    		pch=strtok(NULL, " ");
    		x++;
    	}
    
        return keywords;    
    }
    It compiles and work for a little, then after some pages I get this error:

    Code:
    *** glibc detected *** corrupted double-linked list: 0x08b9b160 ***
    Where 0x08b9b160 alwys change.

    I can't figure out why...any idea?

    Thank you in advance!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    maybe some of the functions that you call, use double linked lists and they get corrupted somehow

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char *no2spaces = new char[(signed)strlen(out)+1];
    > no2spaces=out;
    Maybe, you should stick to std::string rather than messing with char arrays.

    This does NOT copy the string, it merely trashes your pointer to memory you allocated with a pointer to memory you didn't. The result is BOOM when it comes to freeing that memory.

    You should have used
    strcpy( no2spaces, out );

    And why did you cast the result of strlen() ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    2
    Quote Originally Posted by Salem
    > char *no2spaces = new char[(signed)strlen(out)+1];
    > no2spaces=out;
    Maybe, you should stick to std::string rather than messing with char arrays.

    This does NOT copy the string, it merely trashes your pointer to memory you allocated with a pointer to memory you didn't. The result is BOOM when it comes to freeing that memory.

    You should have used
    strcpy( no2spaces, out );

    And why did you cast the result of strlen() ?
    I understand! Thank you very much! I'm not an experienced programmer as you can see...
    I also removed the cast of the strlen result...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked List Problem
    By Shiggins in forum C++ Programming
    Replies: 4
    Last Post: 03-10-2009, 07:15 AM
  2. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM