1

これは私がコンストラクタ用に持っているコードです:

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;

}

「impl」をどこかに保存したいので、関数の最後に返す X に保存しました。しかし、この関数が終了するとすぐに、GC はこのオブジェクトのデコンストラクターを呼び出します。このオブジェクトが呼び出されたときに、GC によって回避されるように設定するにはどうすればよいですか?

編集: インターネットで何時間も検索した後、c++ には固定ポインター (pin_ptr) と呼ばれるものがあることに気付きましたが、その上で見つけたすべての例は、int の配列を内部に保存していることを示しています。固定されたポインター内にオブジェクトを保存することは可能ですか?

4

2 に答える 2

1

C++/CX にはガベージ コレクションはありません。

定義した LmiVideoCapturerWinRTImplementation^ impl 変数は、オブジェクトの有効期間を自動的に管理するスマート ポインター型です。C++/CX 型の詳細については、http: //blogs.msdn.com/b/vcblog/archive/2012/09/17/cxxcxpart02typesthatwearhats.aspxを参照してください。

于 2014-12-12T02:36:25.733 に答える
0

代わりに T^ を返すか、構造体 U で T^ をラップする必要がある場合は、その U を値で返します。

できる限り生のポインタを避けてください。void* にキャストして型情報を失うこともありません。WinRT オブジェクトの場合、最も安全に実行できるのは、Platform::Object^ または IInspectable* にキャストすることです。後者の場合、所有参照を格納するために ComPtr を使用します。

于 2014-12-15T06:52:57.890 に答える