如何将 std::vector<std::vector<int>> 从 C++ 返回到 C++/CLI?
How to return std::vector<std::vector<int>> from C++ to C++/CLI?
在我的解决方案中,我有c++/CLI (vs2012)项目,在c++ (vs2010)项目中执行一些方法。以下是本机代码的签名:
void Pcl::Downsample(std::vector<CloudPointNative>& points, std::vector<std::vector<int>>& clusters)
下面是我如何在c++/CLI端执行它:
std::vector<std::vector<int>> clusters;
pcl->Downsample(points, clusters);
然后我尝试遍历集群:
for (int clusterIndex = 0; clusterIndex < clusters.size(); clusterIndex++)
{
auto cluster = clusters[clusterIndex];
簇的大小为7,向量中的每一项都包含int类型的向量。我可以在本机端的调试器中看到这个。一旦我回到托管端(c++/cli项目),我就会遇到问题。如果clusterIndex == 0, clusterIndex == 5,则正常工作。但是对clusterIndex的任何其他值抛出AccessViolationException。
auto cluster0 = clusters[0]; // works
auto cluster1 = clusters[1]; // AccessViolationException
auto cluster5 = clusters[5]; // works
这是怎么回事?
解决。我已将签名更改为:
std::vector<std::vector<int>*>* Pcl::Downsample(std::vector<CloudPointNative>& points)
并增加了Free方法来删除向量
void Pcl::Free(std::vector<std::vector<int>*>* clusters)
{
for(int i = 0; i < clusters->size(); i++)
{
delete (*clusters)[i];
}
delete clusters;
}
因为在外部DLL中创建的对象也应该在外部DLL中删除
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- std::原子加载和存储都需要吗
- 将对象移动到std::shared_ptr
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 使用一个考虑到std::map中键值的滚动或换行的键
- 如何从 std::atomic 中提取指针 T<T>?
- 为什么 std::unique 不调用 std::sort?
- 使用std::函数映射对象方法
- 可组合的lambda/std::函数与std::可选