I have been developing a piece of software in which I am trying to use lists of structs, and structs containing these lists. What I am finding is that I am losing memory somewhere along the lines. I will give somewhat of an example (I do not wish to post the exact code due to IP issues, but this is exactly what the problem section of the code does):
Expand|Select|Wrap|Line Numbers
- struct s1{
- string a1;
- string a2;
- void print() { stuff here}
- };
- struct s2{
- string a1;
- string a2;
- list<s1> b1;
- list<s1> b2;
- list<s1> b3;
- void print() {
- list<s1>::iterator Lis = b1.begin();
- while(Lis!=b1.end()){
- s1 an = *Lis;
- Lis++;
- an.print(); //and similarly for b2 and b3
- }
- }
- };
- ....
- ....
- s2 getdata(){
- s2 s;
- if(condition){
- s.a1="a1";
- s.a2="a2";
- list<s1> x = get1();
- list<s1>::iterator Lis = x.begin();
- while(Lis!=x.end()){
- s1 an = *Lis;
- Lis++;
- an.print(); //these print correctly
- }
- s.b1=x;
- list<s1> y = get2();
- Lis = y.begin();
- while(Lis!=y.end()){
- s1 an = *Lis;
- Lis++;
- an.print();
- }
- s.b2=y;
- list<s1> z = get3();
- Lis = z.begin();
- while(Lis!=z.end()){
- s1 an = *Lis;
- Lis++;
- an.print();
- }
- s.b3=z;
- }
- s.print(); //this gives a different (and incorrect) result
- return s;
- }
It may be of importance that get1(), get2() and get3() all call the same function with slightly different parameters.
Any advice anyone can give would be greatly appreciated.