Opened 5 years ago
#13338 new Bugs
checked_delete问题
Reported by: | Owned by: | No-Maintainer | |
---|---|---|---|
Milestone: | To Be Determined | Component: | utility |
Version: | Boost 1.65.0 | Severity: | Problem |
Keywords: | Cc: |
Description
环境: Visual studio 2017 boost库编译选项: msvc141,multi,win64,debug,shared demo工程设置: debug, win32
问题源: 《Beyond the C++ STL: an introduction to boost》书上Part I, Library 3, checked_delete章节 如果把 deleter.h, deleter.cpp, to_be_deleted.h三个文件内容整合到一个文件里, 编译后会提示 " warning C4150: 删除指向不完整“to_be_deleted”类型的指针;没有调用析构函数" 运行后调用 deleter::do_it 时会间接调用到 ~to_be_deleted()。 调用 deleter::delete_it 时则不会间接调用到 ~to_be_deleted()。 最后调用完 ~to_be_deleted()程序结束时才报一个异常错误。 很奇怪的地方啊!看来是编译器工作方式的不同导致的吧。。。
code like this:
deleter.h class to_be_deleted; class deleter { public:
void delete_it(to_be_deleted* p); void do_it(to_be_deleted* p);
}; deleter.cpp #include "deleter.h" #include "boost/checked_delete.hpp" void deleter::delete_it(to_be_deleted* p) {
delete p;
} void deleter::do_it(to_be_deleted* p) {
boost::checked_delete(p); typedef char type_must_be_complete[sizeof(T)]; 所有代码放入一个文件,这句居然能编译通过??
} to_be_deleted.h #include <iostream> class to_be_deleted { public:
~to_be_deleted() {
std::cout <<
"I'd like to say important things here, please.";
}
}; Test application #include "deleter.h" #include "to_be_deleted.h" int main() {
to_be_deleted* p = new to_be_deleted; deleter d; d.delete_it(p); d.do_it(p);
}