1
votes

In my solution I have C++/CLI (vs2012) project that executes some method in C++ (vs2010) project. Here is a signature of native code:

    void Pcl::Downsample(std::vector<CloudPointNative>& points, std::vector<std::vector<int>>& clusters)

And here is how I execute it on C++/CLI side:

    std::vector<std::vector<int>> clusters;
    pcl->Downsample(points, clusters);

Then I try to iterate over clusters:

    for (int clusterIndex = 0; clusterIndex < clusters.size(); clusterIndex++)
    {
        auto cluster = clusters[clusterIndex];

The size of clusters is 7 and each item in the vector contains vectors of int. I can see this in debugger on the native side. As soon as I get back to managed side (C++/cli project) I get problems. It works correctly if clusterIndex == 0 and clusterIndex == 5. But throws AccessViolationException on any other values of clusterIndex.

auto cluster0 = clusters[0]; // works
auto cluster1 = clusters[1]; // AccessViolationException
auto cluster5 = clusters[5]; // works

How can this be?

1
Microsoft advises against passing and returning STL types over DLL boundaries, especially between different compilers/versions. - Medinoc
Is the function Downsample and calling it in the same DLL? - pogorskiy
@pogorskiy no, they are different DLLs. - Daniel D
@Medinoc I can change this to any other types. I just have to somehow transfer the integers :) - Daniel D
Then you can just return a pointer to an array of pointers to integers; plus have the DLL provide a deletion function if the allocation is based on new or malloc(). - Medinoc

1 Answers

0
votes

Solved. I have changed signature to:

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

and also added Free method to delete the vectors

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

Because objects created in external DLL should be also deleted in external DLL.