[extension]: g++ '-O2' optimization w/ type_map access thru member template function
Hi,
I'm a happy user of boost::extension library. Thanks Jeremy Pack et al. for
this great work, and hope it'll be accepted by Boost soon. My extension
library is dowloaded from http://boost-extension.blogspot.com/ (August 14,
2008 Link), which works great for me when compiled with "-O0" optimization
flag in g++ (my version: i686-apple-darwin9-g++-4.0.1).
But, when I compile with -O2 and access type_map object through member
template function, g++ seems to optimize away the initialization logic for
'type_map', and I'd get signal assertion of EXC_BAD_ACCESS. Here's my test
program:
65 struct MyDict {
66 boost::extensions::type_map mTypeDict;
67 template< typename T >
68 T & refInstance() { mTypeDict.get<T>(); }
69 };
70
71 typedef std::set<int> IntSetType;
72
73 void test_MyDict_1_mem_data_access() {
74 MyDict testObj;
75 std::cout << "------ 1 " << std:: endl;
76 IntSetType &rIntSet = testObj.mTypeDict.get<IntSetType>();
77 std::cout << "------ 2 " << std:: endl;
78 std::cout << "rIntSet.size() is " << rIntSet.size() <<
std::endl;
79 std::cout << "------ 3 " << std:: endl;
80 rIntSet.insert (123);
81 std::cout << "------ 4 " << std:: endl;
82 std::cout << "rIntSet.size() is now " << rIntSet.size() <<
std::endl ;
83 std::cout << "------ 5 " << std:: endl;
84 }
85
86 void test_MyDict_2_mem_func_access() {
87 MyDict testObj;
88 std::cout << "====== 1 " << std:: endl;
89 IntSetType &rIntSet = testObj.refInstance<IntSetType>();
90 std::cout << "====== 2 " << std:: endl;
91 std::cout << "rIntSet.size() is " << rIntSet.size() <<
std::endl;
92 std::cout << "====== 3 " << std:: endl;
93 rIntSet.insert (123);
94 std::cout << "====== 4 " << std:: endl;
95 std::cout << "rIntSet.size() is now " << rIntSet.size() <<
std::endl ;
96 std::cout << "====== 5 " << std:: endl;
97 }
This test program runs without any problem when compiled with "-O0"; but
with "-O2" compiler flag, "test_MyDIct_1_mem_data_access()" is still run ok
as expect, but "test_MyDict_2_mem_func_access()" halts at line 93. Here's
the output when run under gdb:
------ 1
------ 2
rIntSet.size() is 0
------ 3
------ 4
rIntSet.size() is now 1
------ 5
====== 1
====== 2
rIntSet.size() is 86
====== 3
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x0000005e
std::_Rb_tree
participants (1)
-
Jeffrey Chang