C++: Read access violation (this-> x was nullptr) with Blackmagic DeckLink SDK

106 views Asked by At

I write a little program depending on the Blackmagic Decklink SDK. When I use the DeckLinkInput interface I get the read access violation message "this->dl_input was nulltr". After hours of debugging I have no idea how to fix this issue. Maybe one problem is that i am new to C++. I wrote the following code:

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;
}

Can someone tell me what I am doing wrong? Thank you.

1

There are 1 answers

0
RoQuOTriX On

This function seems wrong to me and returns a possible wrong bool value:

In the first if-clause no return is done which should be return false I would say. Also this dl_input is never updated and therefore always a 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;
} 

Also if you are returning in the "if" part itself you don't need to write if-else because either you return from the if or execute the "else" part. There is no other possibility