Thread: Deque Causeing a crash.

  1. #1
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100

    Deque Causeing a crash.

    ok so i have,

    Code:
    class BillData
    {
        private:
        wxString Name;
        wxString Cost;
        wxDateTime DueDate;
    
        public:
        BillData(wxString tempName, wxString tempCost, wxDateTime tempDate);
        ~BillData() {};
    };
    class BillList
    {
        private:
            std::deque <BillData> theList;
        public:
            void InsertBill(wxString tempName, wxString tempCost, wxDateTime tempTime);
            BillList() {};
            ~BillList(){};
    };
    void BillList::InsertBill(wxString tempName, wxString tempCost, wxDateTime tempTime){
            BillData tempData(tempName, tempCost, tempTime);
            theList.push_back(tempData);
    
    }
    But the .push_back(tempData) causes a segfault. The backtrace brings me to the deque header, and points at
    Code:
    	if (this->_M_impl._M_finish._M_cur != this->_M_impl._M_finish._M_last - 1)
    Sorry, but i'm a Code::Blocks man now.

  2. #2
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    This is also causing a segfault
    Code:
     int  getSize() {if( theList.empty()){ return 0;} else { int temp = theList.size(); return temp;}};
    Sorry, but i'm a Code::Blocks man now.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    And how does the code that calls those functions on BillList look like?

    I can't see anything wrong with what you have posted. (Apart from small unrelated things, like why does getSize() look so complicated and why does it return an int instead of unsigned.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Code:
    if(theString != "" && theCost > 0 && goodValue){
            ((BillDia*)GetParent())->AddBill(theString, theCost);
            ((fincal2Frame*)GetParent())->insertBill(theString, theCost, theDate);
        Close();
        }
        else {
            wxMessageBox("Please Fill Out All Information!");
        }
    and the other

    Code:
    	BillList * tempList;
    	tempList = ((GUIFrame*)GetParent())->getList();
        int tempE = tempList->getSize();
    
    	for(int i = 0; i < tempE; i++){
    
             m_listBox1->Append(tempList->getName(i)+"    $"+tempList->getCost(i));
    	}
    Sorry, but i'm a Code::Blocks man now.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That's some pretty heavy casting going on... Are you sure that these casts are correct? Are you sure that you don't get NULL pointers anywhere? Why are you comparing theCost which should be wxString against 0 (pointer?)?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Also, why does fincal2Frame apparently derive from BillList?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    It doesn't, it derives from GUIFrame which has a protected member of BillList.

    Edit: i see what you're getting at, Fincal2Frame has a handler (of the same name) that passes the info to the protected member. The code that is calling that is a child of BillDia so it needs to reach back to the grandparent frame (Fincal2Frame) and pass it the needed info.
    Last edited by Terran; 07-05-2008 at 07:07 AM.
    Sorry, but i'm a Code::Blocks man now.

  8. #8
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    For some reason, accessing the deque is causing segfaults, and i really need to figure out why
    Last edited by Terran; 07-05-2008 at 12:19 PM.
    Sorry, but i'm a Code::Blocks man now.

  9. #9
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    More detective work seems to show that it's crashing when deque, (or vector) tries to access their internal iterators. Which i can't understand? I hope i havn't stumped the panal on this one!
    Sorry, but i'm a Code::Blocks man now.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Stumped? Hardly. The problem is in your code that you haven't posted yet.
    The fact that you have those horible casts there pretty much says that you're doing something wrong. If you can't do without them then you're design is screwed.

    The problem is almost certainly either object slicing or calling a member function on an deleted, null, or otherwise invalid object pointer.
    But you'd have to show every bit of code that explains all the bits which are not currently shown.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    fair enough, here's all the relevant code,

    Ok then i will post code, attempt to bear with me,

    GUIFrame has a protected variable and an acesscor;

    Code:
    ...
    protected:
    ...
        BillList * m_TheList;
    ...
    public:
        BillList * getList() { return m_TheList;}
    ...
    we also have

    Code:
    class BillData
    {
        public:
        wxString Name;
        wxString Cost;
        wxDateTime DueDate;
    
        BillData(wxString tempName, wxString tempCost, wxDateTime tempDate);
        ~BillData() {};
    };
    class BillList
    {
        private:
            std::vector <BillData>  theList;
        public:
            BillData getData(int pos) { if (theList.size()!=0){return theList[pos];};}
            wxString getName(int i) {return theList[i].Name;};
            wxString getCost(int i) {return theList[i].Cost;};
            int  getSize() {
            if(theList.empty()){
                    return 0;
                }
                else {
                    int temp = theList.size();
                    return temp;
                    }
                };
            void InsertBill(wxString tempName, wxString tempCost, wxDateTime tempTime);
            BillList() {};
            ~BillList(){};
    };
    When the second dialog opens, the constructor calls...

    Code:
    ...
    BillList * tempList;
    templist = ((Fincal2Frame*)GetParent())->getList();
    int tempE = tempList->getSize();
    
    for(int i = 0; i < tempE; i++){
    
        m_listBox1->Append(tempList->getName(i)+" $"+tempList->getCost(i));
    }
    ...
    But when i open the dialog which calls the code show above, i get a segfault that backtraces back to the vector iterators. It doesn't make sense. It doesn't work even if i call it from Fincal2Frame (which inherits GUIFrame!)
    Sorry, but i'm a Code::Blocks man now.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Sorry I re-read my post and it doesn't read like I meant it to. Keep up the learning.

    There are a number of inconsistencies and dangers with these 4 functions: getData, getName, getCost, getSize()
    getSize can simply return theList.size(); There's no need to check for a special case of empty. Your getData function even acknowledges that empty containers have zero items.
    getData should be producing a warning if not an error because it returns nothing if the if-statement is false. Also, checking that it is not empty is not enough to stop the next statement going out of bounds when pos > 0.

    You need to bump up your compiler's warning level. If the warning shown seem like a pain to fix it's only because they're telling you about real issues that you should fix.
    in getName and getCost I would put an assert into them. In fact a great start to tracking down the problem is to either use at() instead of [] or an assert before the [].

    I think one of the critical issues is what does GetParent() do?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    m_TheList - do you ever set this to something?

    it needs to reach back to the grandparent frame
    You only call GetParent() once, so, wouldn't this be the parent frame? Not the grandparent?

    Personally, this is a hack to me. Don't go through GetParent() and rely on the family tree of windows to provide you with your data. If you ever decide to layout those windows differently, or if some other dialog calls that dialog, you'll be screwed. Why not, when you construct the frame/dialog/window, pass a pointer/reference to any data that it will be needing, and store it then?

    Finally, I agree- ditch the .empty() check. Call .empty() if you want to know if a container _has_ items, call .size() when you want to know how many.

    For the forum: This seems to be a wxWidgets app, so... I'm guessing that 'Fincal2Frame' (sic?) and BillDia [log] both derive from wxWindow (not directly). GetParent() is a wxWindow member to get the parent window.
    Edit: Ok, now, I'm wondering...
    Code:
            ((BillDia*)GetParent())->AddBill(theString, theCost);
            ((fincal2Frame*)GetParent())->insertBill(theString, theCost, theDate);
    Is that correct? You parent window is both a fincal2Frame and a BillDia? This would seem incredibly unlikely for a wx app... am I completely missing something?
    Can you give us a better idea of your class heirarchy? The backtrace when the crash occurs?
    Last edited by Cactus_Hugger; 07-06-2008 at 12:41 AM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  14. #14
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Well it would see that the main error was that i overlooked initializing m_TheList in the GUIFrame constructor.

    also, i don't actually use "getData" because it seemed like a bad way to do things.

    as far as
    Code:
     ((BillDia*)GetParent())->AddBill(theString, theCost); //<-this works
            ((fincal2Frame*)GetParent())->insertBill(theString, theCost, theDate); //<-this crashes
    The full code is here, but you do need wxWidgets to run it; you should be able to download the files here. http://www.filefactory.com/file/ff35e5/n/fincal2_zip (of course distributed under the GNU free license!)

    Edit: to make it work, what i did was put the call to insertBill as part of AddBill, now it works well! Now i have to figure out how to save and load all that info from the BillList class to a file!
    Last edited by Terran; 07-06-2008 at 08:36 AM.
    Sorry, but i'm a Code::Blocks man now.

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Unrelated, but any reason you're using wxString instead of std::string?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deque best practices
    By George2 in forum C++ Programming
    Replies: 10
    Last Post: 03-02-2008, 08:11 PM
  2. Using DEQUE within Class Definition
    By wbeasl in forum C++ Programming
    Replies: 8
    Last Post: 10-07-2007, 09:50 AM
  3. Confusion with a Deque program
    By Bluefish in forum C++ Programming
    Replies: 0
    Last Post: 05-20-2006, 03:13 PM
  4. costum grow() not working
    By Opel_Corsa in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2006, 10:11 PM
  5. Very slow processing of vectors?
    By Blackroot in forum C++ Programming
    Replies: 35
    Last Post: 02-06-2006, 06:36 PM