Thread: Strange error during runtime

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    9

    Question Strange error during runtime

    I made a fairly basic character by character string tokenizer. It seems to run fine except I get this strange error at the end of the program:

    *** glibc detected *** ./order: free(): invalid pointer: 0xbffdd42c ***
    ======= Backtrace: =========
    /lib/tls/i686/cmov/libc.so.6[0xb7d30a85]
    /lib/tls/i686/cmov/libc.so.6(cfree+0x90)[0xb7d344f0]
    /usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xb7efcb11]
    /usr/lib/libstdc++.so.6(_ZNSs4_Rep10_M_destroyERKSaIcE+0x1d )[0xb7ed879d]
    /usr/lib/libstdc++.so.6(_ZNSsD1Ev+0x51)[0xb7eda571]
    ./order[0x804ab42]
    /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7cdb450]
    ./order(__gxx_personality_v0+0x69)[0x8048f61]
    ======= Memory map: ========
    08048000-0804f000 r-xp 00000000 08:01 3137610 /home/hewhosurvives/c++/order/order
    0804f000-08050000 rw-p 00006000 08:01 3137610 /home/hewhosurvives/c++/order/order
    08050000-08071000 rw-p 08050000 00:00 0 [heap]
    b7b00000-b7b21000 rw-p b7b00000 00:00 0
    b7b21000-b7c00000 ---p b7b21000 00:00 0
    b7cc4000-b7cc5000 rw-p b7cc4000 00:00 0
    b7cc5000-b7e0e000 r-xp 00000000 08:01 4981802 /lib/tls/i686/cmov/libc-2.7.so
    b7e0e000-b7e0f000 r--p 00149000 08:01 4981802 /lib/tls/i686/cmov/libc-2.7.so
    b7e0f000-b7e11000 rw-p 0014a000 08:01 4981802 /lib/tls/i686/cmov/libc-2.7.so
    b7e11000-b7e14000 rw-p b7e11000 00:00 0
    b7e14000-b7e1e000 r-xp 00000000 08:01 4964364 /lib/libgcc_s.so.1
    b7e1e000-b7e1f000 rw-p 0000a000 08:01 4964364 /lib/libgcc_s.so.1
    b7e1f000-b7e20000 rw-p b7e1f000 00:00 0
    b7e20000-b7e43000 r-xp 00000000 08:01 4981806 /lib/tls/i686/cmov/libm-2.7.so
    b7e43000-b7e45000 rw-p 00023000 08:01 4981806 /lib/tls/i686/cmov/libm-2.7.so
    b7e45000-b7f2d000 r-xp 00000000 08:01 549195 /usr/lib/libstdc++.so.6.0.9
    b7f2d000-b7f30000 r--p 000e8000 08:01 549195 /usr/lib/libstdc++.so.6.0.9
    b7f30000-b7f32000 rw-p 000eb000 08:01 549195 /usr/lib/libstdc++.so.6.0.9
    b7f32000-b7f38000 rw-p b7f32000 00:00 0
    b7f45000-b7f47000 rw-p b7f45000 00:00 0
    b7f47000-b7f48000 r-xp b7f47000 00:00 0 [vdso]
    b7f48000-b7f62000 r-xp 00000000 08:01 4964421 /lib/ld-2.7.so
    b7f62000-b7f64000 rw-p 00019000 08:01 4964421 /lib/ld-2.7.so
    bffc9000-bffde000 rw-p bffeb000 00:00 0 [stack]
    Aborted


    Anyone know how I can track down what is causing it?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> free(): invalid pointer
    Start looking at all your calls to free() (or delete).

    gg

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    I haven't manually called either. Could using vectors and strings set that off?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you call a destructor explicitly?

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    here is most of the relavent code:
    Code:
    //THIS IS THE HEADER FILE
    #include<string>
    #include<fstream>
    #include<vector>
    //I know, don't do this in a header, but at this point I am just trying to get it to work
    using namespace std;
    class item
    {
        public:
        /*
        This constructor accepts a string and creates an object with that name
        @parameter- inputName: The name of the of the item
        */
        item(string inputName);
    
        /*
        This function intializes a blank object
        */
        item();
        /*
        This function returns the name of the item
        @return: The name of the item
        */
        string getName() const;
        /*
        This function outputs the members of item
        @param stream: The stream the output is being written to
        @param inputItem:  The item that is being written to the stream
        @return: The outputstream
        */
        friend ostream &operator<<(ostream &stream, item inputItem);
        /*
        this function outputs the size of the name string followed by the name of the item to the stream given
        @param outputStream:  The stream to be written to
        */
        void save(ofstream &outputStream);
        /*
        This function inputs the size of the name vector and then loads the name vector
        @param inputStream: the file stream to be read from
        @return: Whether the load was successful or not
        */
        bool load(ifstream &inputStream);
    
    
        /*
        This function constructs an item with the contents of another item
        @param inputItem: The item to be copied from
        */
    //    item(item const& inputItem);
        
        /*
        This operator assigns the values of another item to itself and returns a pointer to itself
        @param inputItem:  The item the values are being taken from
        @return:  a pointer to this item
        */
    //    item& operator = (item &inputItem);
        private:
        string name;
    };
    
    
        /*
        This constructor accepts a string and creates an object with that name
        @parameter- inputName: The name of the of the item
        */
    item::item(string inputName)
    {
        name=inputName;
    }
    
        /*
        This function intializes a blank object
        */
        item::item()
    {
    }
    
        /*
        This function returns the name of the item
        @return: The name of the item
        */
    string item::getName() const
    {
        return name;
    }
        /*
        This function outputs the members of item, first the length of the vector and then the vector name
        @param stream: The stream the output is being written to
        @param inputItem:  The item that is being written to the stream
        @return: The outputstream
        */
    ostream &operator<<(ostream &stream, item inputItem)
    {
        stream<<inputItem.name.length() << inputItem.name;
        return stream;
    }
    
     /*
        this function outputs the size of the name string followed by the name of the item to the stream given
        @param outputStream:  The stream to be written to
        */
    void item::save(ofstream &outputStream)
    {
        outputStream << *this;
    }
        /*
        This function inputs the size of the name vector and then loads the name vector
        @param inputStream: the file stream to be read from
        @return: Whether the load was successful or not
        */
        bool item::load(ifstream &inputStream)
        {
            int i;
            char ch;
            if(inputStream)
            {
                inputStream>>i;
            }
            else
            {
                return false;
            }
            for(int ii=0; ii<i && inputStream; ii++)
            {
                inputStream.get(ch);
                name.push_back(ch);
            }
            if(i!=name.length())
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    
        /*
        This function constructs an item with the contents of another item
        @param inputItem: The item to be copied from
        */
    /*
        item::item(item const& inputItem)
        {
        name=inputItem.getName();
        }
      */  
        /*
        This operator assigns the values of another item to itself and returns a pointer to itself
        @param inputItem:  The item the values are being taken from
        @return:  a pointer to this item
        */
    /*
        item& item::operator = (item &inputItem)
        {
        name=inputItem.getName();
        return this*;
        }
    */
    
    
    
    
    //This is a menu made of items
    class itemMenu
    {
    public:
    /*
    This function loads all of the items in the file and saves them in the listOfItems
    @Param inputStream: The file stream that the items are to be read from
    */
    itemMenu(ifstream &inputStream);
    /*
    This loads an empty menu
    */
    itemMenu();
    /*
    This function adds an item to the listOfItems
    @param inputItem: the item to be added
    */
    void addItem(item inputItem);
    /*
    This function erases the item with the given name in the listOfItems
    @param itemName: The name of the item to be deleted
    */ 
    void deleteItem(string itemName);
    
    /*
    This function erases the last item on the menu
    */
    void deleteItem();
    
    /*
    This function couts all of the items in the listOfItems
    */
    void showItems();
    
    /*
    This function saves all of the items in the listOfItems
    @param outputStream: The file stream for the items to be saved to
    */
    void saveMenu(ofstream &outputStream);
    
    /*
    This function returns a copy of an item in the menu and a blank item if the index is wrong
    @param inputIndex:  The index of the item to be accessed
    */
    item getItem(int inputIndex);
    private:
    vector<item> listOfItems;
    
    };
    
    
    
    /*
    This function loads all of the items in the file and saves them in the listOfItems
    @Param inputStream: The file stream that the items are to be read from
    */
    itemMenu::itemMenu(ifstream &inputStream)
    {
    item null;
    listOfItems.push_back(null);
    while(listOfItems[listOfItems.size()-1].load(inputStream))
    {
    listOfItems.push_back(null);
    }
    listOfItems.pop_back();
    }
    /*
    This loads an empty menu
    */
    itemMenu::itemMenu()
    {
    }
    /*
    This function adds an item to the listOfItems
    @param inputItem: the item to be added
    */
    void itemMenu::addItem(item inputItem)
    {
    listOfItems.push_back(inputItem.getName());
    }
    /*
    This function erases the item with the given name in the listOfItems
    @param itemName: The name of the item to be deleted
    */ 
    void itemMenu::deleteItem(string itemName)
    {
    for(int i=0; i<listOfItems.size(); i++)
    {
    if(listOfItems[i].getName()==itemName)
    {
    listOfItems.erase(listOfItems.begin()+i-1);
    }
    }
    }
    
    /*
    This function erases the last item on the menu
    */
    void itemMenu::deleteItem()
    {
    listOfItems.pop_back();
    }
    
    /*
    This function couts all of the items in the listOfItems
    */
    void itemMenu::showItems()
    {
    for(int i=0; i<listOfItems.size(); i++)
    {
    cout << listOfItems[i];
    }
    }
    
    /*
    This function saves all of the items in the listOfItems
    @param outputStream: The file stream for the items to be saved to
    */
    void itemMenu::saveMenu(ofstream &outputStream)
    {
    for(int i=0; i<listOfItems.size(); i++)
    {
    listOfItems[i].save(outputStream);
    }
    }
    
    /*
    This function returns a copy of an item in the menu and a blank item if the index is wrong
    @param inputIndex:  The index of the item to be accessed
    */
    item itemMenu::getItem(int inputIndex)
    {
    if(inputIndex<listOfItems.size())
    {
    return listOfItems.at(inputIndex);
    }
    else
    {
    item noName;
    return noName;
    }
    }
    
    class burgerMan
    {
    public:
    /*
    This function load's burgerMan's menu from the file so he can take orders
    @param inputStream: the file stream with the menu items
    */
    burgerMan(ifstream &inputStream);
    /*
    This function takes in a natural language restarant order and extracts the which order types are required and how much of each
    @param inputString: The natural langauge request
    @return: The order
    */
    string takeOrder(string inputString);
    private:
    itemMenu menu;
    };
    
    /*
    This function load's burgerMan's menu from the file so he can take orders
    @param inputStream: the file stream with the menu items
    */
    burgerMan::burgerMan(ifstream &inputStream):menu(inputStream)
    {
    }
    /*
    This function takes in a natural language restarant order and extracts the which order types are required and how much of each
    @param inputString: The natural langauge request
    @return: The order
    */
    string burgerMan::takeOrder(string inputString)
    {
    bool numberActive=false;
    vector<string> tokens;
    bool whiteSpace=true;
    string buffer;
    //tokenizing
    for(int i=0; i<inputString.size();i++)
    {
    if(inputString[i]!=' ')
    {
    if(whiteSpace)
    {
    buffer.append(1,inputString[i]);
    tokens.push_back(buffer);
    whiteSpace=false;
    }
    else
    {
    tokens[tokens.size()-1].append(1,inputString[i]);
    }
    }
    else
    {
    whiteSpace=true;
    buffer="";
    }
    }
    for(int i=0; i<tokens.size(); i++)
    {
    cout << tokens[i]<<i << "\n";
    }
    
    }
     //THIS IS THE MAIN FILE
    #include<fstream>
    #include<iostream>
    #include<string>
    #include</home/hewhosurvives/c++/order/item.h>
    #include</home/hewhosurvives/c++/order/functions.h>
    using namespace std;
    int main(int argc, char* argv[])
    {
    
    ifstream streamer("orderMenu");
    burgerMan mani(streamer);
    mani.takeOrder("This is a sentice.   this not.  Can you tell the difference?");
    cout <<"\n";
    return 0;
    }
    The takeOrder function of the burgerMan class has most of the code that actually get exicuted
    Last edited by JamesKidder; 11-16-2008 at 07:25 PM. Reason: forgot to capitalize burgerMan

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your takeOrder is supposed to return a string, but doesn't. Once I fix that to return void (VS wouldn't even let me compile it otherwise), I don't get a crash. Maybe I'll wander over to the mac, which is more like the setting you're in, and see whether that makes a difference.

    Edit: Same deal here -- I get a segmentation fault if I run your code; if I change takeOrder to return void, everything's fine. Apparently it's the trying to destruct a string that you didn't return that is causing the problems. So if you do intend to return a string, later, then for now just return something (anything); otherwise change it to void. And read your compiler warnings!
    Last edited by tabstop; 11-16-2008 at 07:45 PM.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    That did it. Wow, GCC compiled that without error. Thanks.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, if by without error you mean:
    Code:
    temp.cpp: In member function ‘bool item::load(std::ifstream&)’:
    temp.cpp:129: warning: comparison between signed and unsigned integer expressions
    temp.cpp: In member function ‘void itemMenu::deleteItem(std::string)’:
    temp.cpp:251: warning: comparison between signed and unsigned integer expressions
    temp.cpp: In member function ‘void itemMenu::showItems()’:
    temp.cpp:273: warning: comparison between signed and unsigned integer expressions
    temp.cpp: In member function ‘void itemMenu::saveMenu(std::ofstream&)’:
    temp.cpp:285: warning: comparison between signed and unsigned integer expressions
    temp.cpp: In member function ‘item itemMenu::getItem(int)’:
    temp.cpp:297: warning: comparison between signed and unsigned integer expressions
    temp.cpp: In member function ‘std::string burgerMan::takeOrder(std::string)’:
    temp.cpp:345: warning: comparison between signed and unsigned integer expressions
    temp.cpp:366: warning: comparison between signed and unsigned integer expressions
    temp.cpp:340: warning: unused variable ‘numberActive’
    temp.cpp:371: warning: control reaches end of non-void function
    (I put everything in one file, so your line numbers may vary.) But the one at the end is the big one (this time).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runtime formation and execution at runtime
    By Soham in forum C Programming
    Replies: 17
    Last Post: 08-27-2008, 08:45 AM
  2. Replies: 4
    Last Post: 03-26-2008, 08:48 AM
  3. link with C runtime library
    By George2 in forum C++ Programming
    Replies: 26
    Last Post: 02-05-2008, 01:56 AM
  4. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  5. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM