How can I save a object, so that it won't get collected by the GC in C++/CX

98 views Asked by At

This is the code that I have for the constructor:

LmiVideoCapturer* LmiVideoCapturerConstruct_(LmiVideoCapturer* x, const void* implementation)
{
std::vector<LmiVideoCapturerInfo> &deviceList = LmiVideoCapturerDeviceList::Instance();
LmiVideoCapturerInfo &capturerInfo = LmiVideoCapturerInfo();
for (std::vector<LmiVideoCapturerInfo>::iterator it = deviceList.begin(); it != deviceList.end(); it++){
    if (LmiStringCompare(&it->uniqueId, &x->uniqueId) == 0){
        capturerInfo = *it;
        break;
    }
}

if (capturerInfo.uniqueId.size > 0){
    x->isBuiltin = LMI_TRUE;

    // set basic device info 
    LmiStringAssign(&x->name, &capturerInfo.name);
    LmiStringAssign(&x->model, &capturerInfo.model);
    LmiStringAssign(&x->manufacturer, &capturerInfo.manufacturer);
    x->position = capturerInfo.position;

    // set video capabilities
    LmiAllocator *a = LmiMallocAllocatorGetDefault();
    Platform::String ^deviceId = LmiStringWinRTString(&capturerInfo.uniqueId, a);
    XTRACE(L"=========================Will call from LMIVideoCapturerConstruct\n");
    LmiVideoCapturerWinRTImplementation ^impl = ref new LmiVideoCapturerWinRTImplementation(deviceId);
    if (impl->Initialize()){
        //TODO will need to save impl inside a pin_ptr (pinned pointer) so it will not be deconstructed by the GC
        x->implementation = reinterpret_cast<void*>(impl);
        LmiVideoCapturerCapability capability;
        LmiVideoCapturerCapabilityConstructDefault(&capability, a);
        capability.height = impl->encodingProfile->Video->Height;
        capability.width = impl->encodingProfile->Video->Width;
        LmiMediaFormat format; 
        LmiMediaFormatConstructFromNative(&format, impl->encodingProfile->Video->ProfileId);
        LmiVectorPushBack(LmiMediaFormat)(&capability.formats, &format);
        double usecs = ((double)impl->encodingProfile->Video->FrameRate->Denominator / impl->encodingProfile->Video->FrameRate->Numerator) * LMI_USECS_PER_SEC;
        LmiTimeRange range;
        LmiTimeRangeConstruct(&range, LmiTimeUsecs(usecs), LmiTimeUsecs(usecs));
        LmiVectorPushBack(LmiTimeRange)(&capability.ranges, &range);
        LmiVectorPushBack(LmiVideoCapturerCapability)(&x->capabilities, &capability);

        return x;
    }

}

return nullptr;

}

Now I want to save "impl" somewhere, thats why I saved it in X that I will return at the end of the function. BUT as soon as this function is ended, a GC calls the deconstructor for this object. How can I set this object to be avoided by the GC, when it is called?

EDIT: After hours of searching on the internet I noticed that c++ has something called pinned pointers (pin_ptr) but all the examples I found on it, shows saving arrays of int inside. Is it possible to save a object inside a pinned pointer?

2

There are 2 answers

2
robwirving On BEST ANSWER

There is no garbage collection in C++/CX.

The LmiVideoCapturerWinRTImplementation^ impl variable you defined is a smart pointer type that will automatically manage the lifetime of the object for you. More information on C++/CX types can be found here: http://blogs.msdn.com/b/vcblog/archive/2012/09/17/cxxcxpart02typesthatwearhats.aspx

1
CÅdahl On

Return a T^ instead, or if you need to wrap the T^ in a struct U, just return that U by value.

Avoid raw pointers as far as you can. Don't lose type information by casting to void* either. For a WinRT object, the most you can do safely is to cast to Platform::Object^ or IInspectable*. In the latter case, use ComPtr for storing an owning reference.