当托管句柄保存在托管容器(IList <foo ^>)中时,是否有必要在C ++ / CLI中使用GC :: KeepAlive?</foo ^>

时间:2011-04-12 20:03:56

标签: visual-c++ c++-cli managed-c++

当我需要在C ++ / CLI包装器代码中使用KeepAlive以及如何处理生命周期时,我感到很困惑。请考虑以下代码并记下我询问是否需要KeepAlive的地方。

// convert from managed to native string
inline std::string ToStdString(String^ source)
{
    if (String::IsNullOrEmpty(source))
        return std::string();

    int len = ((source->Length+1) * 2);

    /*** Do I need GC::KeepAlive(source) here? ***/

    char *ch = new char[ len ];
    bool result ;
    {
        pin_ptr<const wchar_t> wch = PtrToStringChars( source );
        result = wcstombs( ch, wch, len ) != -1;
    }
    std::string target = ch;
    delete ch;
    if(!result)
        throw gcnew Exception("error converting System::String to std::string");
    return target;
}

// convert from native to managed string
inline String^ ToSystemString(const std::string& source)
{
    return gcnew String(source.c_str());
}

// unmanaged C++ class
struct NativeDog
{
    std::string name;
    std::string bark() const {return "woof";}
    void eat(std::string& food) const {food.clear();}
};

typedef shared_ptr<NativeDog> NativeDogPtr;

// C++/CLI wrapper class
ref class ManagedDog
{
    NativeDogPtr* base_;
    NativeDog& base() {return **base_;}
    ManagedDog() {base_ = new NativeDogPtr(new NativeDog);}
    ~ManagedDog() {if (base_) delete base_;}
    !ManagedDog() {delete this;}

    property String^ name
    {
        String^ get() {return ToSystemString(base().name);}
        void set(String^ name)
        {
              base().name = ToStdString(name);
              /*** Do I need GC::KeepAlive(name) here? ***/
        }
    }

    String^ bark() {return ToSystemString(base().bark());}
    void eat(String^ food)
    {
         std::string nativeFood = ToStdString(food);
         base().eat(nativeFood);
         food = ToSystemString(nativeFood);
         /*** Do I need GC::KeepAlive(food) here? ***/
    }
};

// unmanaged C++ class
struct NativeKennel
{
    vector<NativeDogPtr> dogs;
};

// C++/CLI wrapper class
ref class ManagedKennel
{
    NativeKennel* base_;
    NativeKennel& base() {return *base_;}
    IList<ManagedDog^>^ dogs;
    void addDog(ManagedDog^ dog)
    {
        base().dogs.push_back(*dog->base_);
        dogs->Add(dog);
        /*** Do I need GC::KeepAlive(dog) here? Will the IList manage the ManagedDog lifetimes? ***/
    }
};

2 个答案:

答案 0 :(得分:3)

  

在调用托管委托的函数指针之前。

这是一种常见的故障模式,垃圾收集器无法看到本机代码所持有的任何引用。托管代码必须存储对委托本身的引用,以防止它被垃圾收集。有一个调试器助手,不知道为什么你没有看到它。有关此MSDN Library article

的详细信息

答案 1 :(得分:1)

以上都不是!

如果您在C ++ / CLI中访问托管类,KeepAlive将无济于事。您需要将数据固定在内存中以防止它重新定位垃圾收集。在所有这些示例中,这是由您调用的函数隐式完成的。

KeepAlive有不同的目标。存储在堆栈上的引用在最后一次取消引用对象后立即进行垃圾回收。 KeepAlive通过延长对象的生命周期直到KeepAlive调用之后来防止这种情况发生。