C++:使用 Blackmagic DeckLink SDK 读取访问冲突this-> x was nullptr

问题描述

我根据 Blackmagic Decklink SDK 编写了一个小程序。当我使用 DeckLinkInput 接口时,我收到读取访问冲突消息“this->dl_input was nulltr”。 经过数小时的调试,我不知道如何解决这个问题。也许一个问题是我是 C++ 的新手。 我写了以下代码

class ControlVideo {
    
public:
    HRESULT result;
    
    IDeckLink *deckLink = nullptr;
    IDeckLinkInput *dl_input = nullptr;
    IDeckLinkInputCallback *theCallback = nullptr;
    IDeckLinkdisplayMode *dl_displayMode = nullptr;
    IDeckLinkdisplayModeIterator* dl_displayModeIterator = nullptr;
            
    bool inputCallback(IDeckLinkInput* dl_input);
    bool startCapture();
};
    
/**
 * Function that initializes the input callback
 * The callback will be called for each incoming frame
 */
bool ControlVideo::inputCallback(IDeckLinkInput *dl_input) {
        
    if (dl_input == nullptr) {
        std::cout << "DECKLINK INPUT IS NULLPOINTER.";
    }
    else {
        if (dl_input->SetCallback(theCallback) == S_OK) {
            std::cout << "Input Callback called. \n";
            return true;
        }
        else {
            std::cout << "DeckLink Callback fails. \n";
            return false;
        }
    }
} 
    
/**
 * Function that start the stream
 */
bool ControlVideo::startCapture() {
    
    ControlVideo ctrlVideo;
    
    //make a callback for each incoming frame
    ctrlVideo.inputCallback(dl_input);
    
    if (dl_input->StartStreams() == S_OK) {
        std::cout << "Stream has been started. \n";
        return true;
    }
    else {
        throw std::runtime_error("Error: Stream can not been started. \n");
        return false;
    }
}
    
    
int main() {
    //Create object and initialize the DeckLink
    InitializeDecklink init;
    init.initializeDevice();
    init.printDevice();
    
    ControlVideo ctrlVideo;
    ctrlVideo.startCapture();
        
    return 0;
}

谁能告诉我我做错了什么?谢谢。

解决方法

这个函数在我看来是错误的并且返回了一个可能错误的 bool 值:

在第一个 if 子句中没有返回,我会说应该返回 false。此外,此 dl_input 永远不会更新,因此始终为 nullptr。

/**
 * Function that initializes the input callback
 * The callback will be called for each incoming frame
 */
bool ControlVideo::inputCallback(IDeckLinkInput *dl_input) {
        
    if (dl_input == nullptr) {
        std::cout << "DECKLINK INPUT IS NULLPOINTER.";
        return false; // this is missing
    }
    if (dl_input->SetCallback(theCallback) == S_OK) {
         std::cout << "Input Callback called. \n";
         return true;
    }
    std::cout << "DeckLink Callback fails. \n";
    return false;
} 

此外,如果您在“if”部分本身返回,则不需要编写 if-else,因为要么从 if 返回,要么执行“else”部分。没有别的可能