strange std::bad_alloc error

When I add the following code ,the memory increase dramaticly ,finally the error occurs:std::bad_alloc.
But I can not find the overflow, Can anyone show me the errors??


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//class defination
class t_satObs {
 public:
  t_satObs() {}
  t_satObs(const t_satObs& old) { // copy constructor (deep copy)
    _staID = old._staID;
    _prn   = old._prn;
    _time  = old._time;
    for (unsigned ii = 0; ii < old._obs.size(); ii++) {
      _obs.push_back(new t_frqObs(*old._obs[ii]));
    }
  }
  ~t_satObs() {for (unsigned ii = 0; ii < _obs.size(); ii++) delete _obs[ii];}

  std::string            _staID;
  t_prn                  _prn;
  bncTime                _time;
  std::vector<t_frqObs*> _obs;
};
//The possible error codes:
while(true)
{
   if (_rnx)
	{
		long iSec    = long(floor(obs._time.gpssec()+0.5));
		if (_rnx->samplingRate() == 0 || iSec % _rnx->samplingRate() == 0)
		{
			_rnx->deepCopy(obs);
		}
		_rnx->dumpEpoch(format, obs._time);
	}
} 


//function
void deepCopy(t_satObs obs) {
  _obs.push_back(obs);  //_obs:member of the object :_rnx,  defination:QList<t_satObs> _obs;
}

void dumpEpoch(const QByteArray& format, const bncTime& maxTime) {
  QList<t_satObs> obsList;
  QMutableListIterator<t_satObs> mIt(_obs);
  while (mIt.hasNext()) {
    t_satObs obs = mIt.next();
    if (obs._time < maxTime) {
      obsList.push_back(obs);
	  mIt.remove();
    }
  }
}
Last edited on
You don't have enough information here to point out anything particular, although your use of pointers and new in the t_satObs class suggests that you don't understand object ownership/pointers as well as you should. (Where is that type's assignment operator?)
Last edited on
when I add the assignment operator,the memory is also increasing!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
t_satObs& t_satObs::operator=(const t_satObs &old)
{
	if (this == &old)
	{
		return *this;
	}

	if (_obs.size()>0)
	{
		for (unsigned ii = 0; ii < old._obs.size(); ii++) {
			delete _obs[ii];
		}
	}

	for (unsigned ii = 0; ii < old._obs.size(); ii++) {
		_obs.push_back(new t_frqObs(*old._obs[ii]));
	}
	return *this;
}

Topic archived. No new replies allowed.