Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: vectorName.clear() causing problems?  (Read 2361 times)

0 Members and 1 Guest are viewing this topic.

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
vectorName.clear() causing problems?
« on: July 25, 2013, 04:24:34 pm »
This works:
for (unsigned int i = 0; i < 30; ++i)
            {

EditBoxNames[i] = Group1->get("Ime" + tgui::to_string(i));  // call EditBox named Ime
GroupNames.push_back(EditBoxNames[i]->getText());         // Take values from
saveGroup1Names(GroupNames);    // call function to save Names to file

EditBoxLastNames[i] = Group1->get("Prezime" + tgui::to_string(i));
GroupLastNames.push_back(EditBoxLastNames[i]->getText());
saveGroup1LastNames(GroupLastNames);

EditBoxBrojTelefona[i] = Group1->get("BrojTelefona" + tgui::to_string(i));
GroupBrojTelefona.push_back(EditBoxBrojTelefona[i]->getText());
saveGroup1BrojTelefona(GroupBrojTelefona);

EditBoxDatumRodjenja[i] = Group1->get("DatumRodjenja" + tgui::to_string(i));
GroupDatumRodjenja.push_back(EditBoxDatumRodjenja[i]->getText());
saveGroup1DatumRodjenja(GroupDatumRodjenja);

TextBoxGrupaNapomene[i] = Group1Napomene->get("Group1NapomeneUcenik" + tgui::to_string(i));
GroupNapomene.push_back(TextBoxGrupaNapomene[i]->getText());
saveGroup1Napomene(GroupNapomene);

}
 

After that i called:
// here was for loop from prevoius code

// here was cout about vector size and capacity [ see picture ]
EditBoxNames.clear();
EditBoxLastNames.clear();
EditBoxBrojTelefona.clear();
EditBoxDatumRodjenja.clear();
// here was cout about new size and capacity [see picture ]

// new for loop starts here
 
So i clear those vectors to use them again in new for loop [ if needed i will post new for loop ] . And just before new for loop program crashes with core dumped. dbg says:
-------------------------------------------------------------------
nr: Adress:        Func:    File:
#0 0xb7d587b8 ??()     (/lib/i386-linux-gnu/libc.so.6:??)
#1 ??             ?? ()    (??:??)
--------------------------------------------------------------------
picture:  http://s16.postimg.org/ro920dvd1/Screenshot_from_2013_07_25_16_18_44.png .

Have i done vectorName.clear wrong? Or it is something else?

Using gcc 4.6.3
code blocks 12.11
elementary os Luna Beta 2

if need more details ask and i will post

thank you in advance
wmbuRn


EDIT: When i comment entire new for loop with /* */ program doesnt crash. And it does do whatever is suposed to do. New for loop uses same vectors just like for loop i posted
« Last Edit: July 25, 2013, 04:31:46 pm by wmbuRn »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: vectorName.clear() causing problems?
« Reply #1 on: July 25, 2013, 05:18:18 pm »
Since you identified that something is probably wrong in your "new for loop" (obviously, the error happens after the cout after the clear) you could have at least showed us that part of code. ???

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
Re: vectorName.clear() causing problems?
« Reply #2 on: July 25, 2013, 05:44:11 pm »
This is how they are declared as global variables.
std::vector<sf::String> GroupNames;
std::vector<tgui::EditBox::Ptr> EditBoxNames;
std::vector<sf::String> GroupLastNames;
std::vector<tgui::EditBox::Ptr> EditBoxLastNames;
std::vector<sf::String> GroupBrojTelefona;
std::vector<tgui::EditBox::Ptr> EditBoxBrojTelefona;
std::vector<sf::String> GroupDatumRodjenja;
std::vector<tgui::EditBox::Ptr> EditBoxDatumRodjenja;
std::vector<sf::String> GroupNapomene;
std::vector<tgui::TextBox::Ptr> TextBoxGrupaNapomene;
 


for (unsigned int i = 0; i < 30; ++i)
            {

            EditBoxNames[i] = Group2->get("Ime" + tgui::to_string(i));
            GroupNames.push_back(EditBoxNames[i]->getText());
            saveGroup2Names(GroupNames);

            EditBoxLastNames[i] = Group2->get("Prezime" + tgui::to_string(i));
            GroupLastNames.push_back(EditBoxLastNames[i]->getText());
            saveGroup2LastNames(GroupLastNames);

            EditBoxBrojTelefona[i] = Group2->get("BrojTelefona" + tgui::to_string(i));
            GroupBrojTelefona.push_back(EditBoxBrojTelefona[i]->getText());
            saveGroup2BrojTelefona(GroupBrojTelefona);

            EditBoxDatumRodjenja[i] = Group2->get("DatumRodjenja" + tgui::to_string(i));
            GroupDatumRodjenja.push_back(EditBoxDatumRodjenja[i]->getText());
            saveGroup2DatumRodjenja(GroupDatumRodjenja);

            TextBoxGrupaNapomene[i] = Group2Napomene->get("Group2NapomeneUcenik" + tgui::to_string(i));
            GroupNapomene.push_back(TextBoxGrupaNapomene[i]->getText());
            saveGroup2Napomene(GroupNapomene);

            }

 

It is almost the same loop as before. Except this loop calls Group2 instead Group1

EDIT: Give me a second to test code again i havent erased all of the vectors. :)

I cleared all the vector i am using [10 of them)
EditBoxNames.clear();
            EditBoxLastNames.clear();
            EditBoxBrojTelefona.clear();
            EditBoxDatumRodjenja.clear();
            TextBoxGrupaNapomene.clear();
            GroupNames.clear();
            GroupLastNames.clear();
            GroupDatumRodjenja.clear();
            GroupBrojTelefona.clear();
            GroupNapomene.clear();
 

Same error happends
« Last Edit: July 25, 2013, 05:52:45 pm by wmbuRn »

Atani

  • Newbie
  • *
  • Posts: 30
    • MSN Messenger - atanisoft@hotmail.com
    • AOL Instant Messenger - AtaniSYSOP
    • Yahoo Instant Messenger - atanisoft
    • View Profile
    • Email
Re: vectorName.clear() causing problems?
« Reply #3 on: July 25, 2013, 05:47:35 pm »
not having a complete example of the code or any of its dependencies makes it hard to determine the cause of the exception here...

However, something that I can suggest is verify that Group1/Group2 are not-null and that the Get method returns something not-null before you attempt to use it (one headache of using pointers everywhere)

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: vectorName.clear() causing problems?
« Reply #4 on: July 25, 2013, 05:52:29 pm »
Obivously when you clear vectors, you can't access them with [ i] anymore.
« Last Edit: July 25, 2013, 05:54:07 pm by G. »

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
Re: vectorName.clear() causing problems?
« Reply #5 on: July 25, 2013, 05:54:23 pm »
Quote
However, something that I can suggest is verify that Group1/Group2 are not-null and that the Get method returns something not-null before you attempt to use it (one headache of using pointers everywhere)
Group1 and Group2 works as they should. :)




I want to remove items that are stored in vectors after first for loop, so i can use same vectors to input new data, instead of creating new vectors.

So i should go with vectorName.erase(vectorName.begin(), vectorName.end()) ?

i did .erase on all vectors, same error
« Last Edit: July 25, 2013, 06:05:53 pm by wmbuRn »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: vectorName.clear() causing problems?
« Reply #6 on: July 25, 2013, 06:09:33 pm »
After clearing the vector, it is empty. Therefore the_vector[i] accesses an invalid element. You must push_back to add elements to your cleared vector.
Laurent Gomila - SFML developer

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
Re: vectorName.clear() causing problems?
« Reply #7 on: July 25, 2013, 06:31:19 pm »
OK. Thank you guys. :)

 

anything