2013-06-19 115 views
1

在我的解決方案中,我有C++/CLI(vs2012)項目,它在C++(vs2010)項目中執行一些方法。 下面是本機代碼簽名:如何從C++向C++/CLI返回std :: vector <std :: vector <int>>?

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 

這是怎麼回事?

+1

Microsoft建議不要在DLL邊界上傳遞和返回STL類型,尤其是在不同的編譯器/版本之間。 – Medinoc

+0

函數'Downsample'並在同一個DLL中調用它? – pogorskiy

+0

@pogorskiy不,它們是不同的DLL。 –

回答

0

已解決。我已經改變了簽名:

std::vector<std::vector<int>*>* Pcl::Downsample(std::vector<CloudPointNative>& points) 

,並還增加了免費的方法來刪除載體

void Pcl::Free(std::vector<std::vector<int>*>* clusters) 
{ 
    for(int i = 0; i < clusters->size(); i++) 
    { 
     delete (*clusters)[i]; 
    } 
    delete clusters; 
} 

由於外部DLL創建的對象應該在外部DLL也被刪除。

相關問題