【问题标题】:C++: Read access violation (this-> x was nullptr) with Blackmagic DeckLink SDKC++:使用 Blackmagic DeckLink SDK 读取访问冲突(this-> x was nullptr)
【发布时间】:2021-06-16 12:33:36
【问题描述】:

我根据 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;
}

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

【问题讨论】:

  • 你是在哪一行代码中得到这个消息的?
  • 我在“if (dl_input->StartStreams() == S_OK)”行收到了消息

标签: c++ nullptr blackmagic decklink read-access


【解决方案1】:

这个函数对我来说似乎是错误的,并返回一个可能错误的 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”部分。没有其他可能

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-25
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    相关资源
    最近更新 更多