std::function 复制和内存损坏
std::function copy and memory corruption
我的程序内存损坏。 driver_s2lp::_conf.rxtx
对象在我的程序中一段时间后被损坏(涉及一些 new
和 delete
)。
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){};
}
我想知道像我在 s2lp_custom_ikm
class 的构造函数中那样复制 std::function
是否有问题?
我不确定它是否相关,但当我在调试器中添加观察点时,m_invoker
在 std::function
对象中被损坏。
问题可能出在其他地方,但我想确定副本不是问题的根源。
I wonder if there is something wrong with copying std::function as I do in the constructor of the s2lp_custom_ikm
class?
没有
构造函数(初始化程序和主体)中的 std::function
个副本是安全的。
显示的程序片段不包含可导致您报告的症状的错误。
我的程序内存损坏。 driver_s2lp::_conf.rxtx
对象在我的程序中一段时间后被损坏(涉及一些 new
和 delete
)。
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){};
}
我想知道像我在 s2lp_custom_ikm
class 的构造函数中那样复制 std::function
是否有问题?
我不确定它是否相关,但当我在调试器中添加观察点时,m_invoker
在 std::function
对象中被损坏。
问题可能出在其他地方,但我想确定副本不是问题的根源。
I wonder if there is something wrong with copying std::function as I do in the constructor of the
s2lp_custom_ikm
class?
没有
构造函数(初始化程序和主体)中的 std::function
个副本是安全的。
显示的程序片段不包含可导致您报告的症状的错误。