Reputation: 688
I have a memory corruption in my program. The driver_s2lp::_conf.rxtx
object gets corrupted, after a while (involving some new
and delete
) in my program.
class driver_s2lp
{
public:
struct conf
{
std::function<std::vector<uint8_t>(std::vector<uint8_t> &data, size_t len)> rxtx;
//there are other variables here
};
driver_s2lp::driver_s2lp(conf config)
{
_conf = config;
}
private:
conf _conf;
};
class s2lp_custom_ikm : private driver_s2lp
{
struct conf
{
std::function<std::vector<uint8_t>(std::vector<uint8_t> &data, size_t len)> rxtx;
//there are other variables here
};
/* can I safely copy the std::function like this ? */
s2lp_custom_ikm(conf configuration) : driver_s2lp([configuration]() {
struct driver_s2lp::conf driver_conf;
driver_conf.rxtx = configuration.rxtx;
// other copies here
return driver_conf;
}())
{
this->configuration = configuration;
}
void do_something()
{
// it seems that driver_s2lp::_conf.rxtx can be broken here
}
};
int main()
{
s2lp_custom_ikm::conf s2lp_config;
s2lp_config.debug = [](std::string s) { printf("%s",s.c_str()); };
//other std::functions here
s2lp = new s2lp_custom_ikm(s2lp_config);
s2lp->do_something()
while(1){};
}
I wonder if there is something wrong with copying std::function
as I do in the constructor of the s2lp_custom_ikm
class?
I'm not sure it is relevant but it is the m_invoker
which gets corrupted in the std::function
object when I add a watchpoint in the debugger.
The problem may be somewhere else but I want to be sure the copy is not the source of the problem.
Upvotes: 0
Views: 251
Reputation: 17454
I wonder if there is something wrong with copying std::function as I do in the constructor of the
s2lp_custom_ikm
class?
No.
The std::function
copies in your constructor (both the initialiser and the body) are safe.
The program snippet shown does not contain a bug capable of the symptoms you report.
Upvotes: 2