YAML::Node operator= and reference behaviour
我对yaml-cpp中节点分配的语义有些困惑。我以为Node类中内置了自动引用计数,但是现在我不
谁能向我解释以下内容(注意声明" temp"节点的范围):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | auto content = std::string{"Test Scalar" }; // Case [1] YAML::Node n1; { YAML::Node temp(content); n1[content] = temp; // Assign temp node as map value } // <temp local variable destroyed here> std::cout << YAML::Dump(n1) << std::endl; // No problem // Case [2] YAML::Node n2; { YAML::Node temp(content); n2[temp] = 1; // Use temp node as map key } // <temp local variable destroyed here> std::cout << YAML::Dump(n2) << std::endl; // Crash, key node memory has been freed // Case [3] YAML::Node n3, n4; { YAML::Node temp(content); n3[content] = temp; // Assign temp node as map value n4[temp] = 1; // Use temp node as map key } // <temp local variable destroyed here> std::cout << YAML::Dump(n3) << std::endl; // No problem std::cout << YAML::Dump(n4) << std::endl; // No problem!! |
为什么[1]可以,但是[2]不可以?
似乎在[3]中,将temp分配给n3 [content]可以防止在销毁temp时释放由temp局部变量引用的节点数据。因此,似乎Node类确实引用了计数,但是当将节点用作映射键时,计数不会增加。我在这里误会了什么吗?
这是yaml-cpp中的错误,现已修复。您是正确的,因为在任何情况下都不会崩溃。
在项目页面上查看问题。