memory corruption when using c++ templates
Question
template<typename V>
class Counter
: public boost::enable_shared_from_this<Counter<V> > {
public:
Counter(boost::asio::io_service& io_service, uint32_t update_interval)
: current_load_(std::numeric_limits<V>::min()),
last_load_(std::numeric_limits<V>::min()),
max_load_value_(std::numeric_limits<V>::max()),
update_timer_(CreateTimer(io_service)),
update_interval_(update_interval) {
}
void Start() {
update_timer_->Start(boost::bind(&SingleLoadCounter::UpdateLoad,
this->shared_from_this(), _1),
boost::posix_time::milliseconds(update_interval_));
}
void Stop() {
update_timer_->Stop();
}
private:
void UpdateLoad(std::function<void(bool redial)> callback) {
{
boost::unique_lock<boost::mutex> lock(mutex_);
last_load_ = current_load_;
current_load_ = 0;
}
callback(true);
}
V current_load_;
V last_load_;
V max_load_value_;
TimerPtr update_timer_;
uint32_t update_interval_;
boost::mutex mutex_;
};
After creating instance of this class in boost::shared_ptr
address update_timer_.px
is valid. But then when in Stop() I call method on update_timer_
I get Segmentation fault, because update_timer_.px
points on the wrong place.
Addresses of update_timer_.px
:
- In constructor of
Counter
: 0x1b15b50 - In method
Stop
: 0x1b15b3000000000
Running it under gdb, I saw that 0x1b15b3 was the address of update_timer_.pn
. So somehow this->update_timer_.px
moved on few bits forward
All this was done on Release target. On Debug it works fine. Class Counter
was used as a shared_prt
with template type uint32_t
System: Ubuntu 10.4 x64, gcc: 4.7.2
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow