none
Access violation with depth data C++ RRS feed

  • Question

  • Hi,

    I tried to load the depth data and save it to an array. After a short period I receive an access violation error :(

    How can I fix it? I'm using Kinect for Windows with the current SDK

    Thanks :)

    #include <Windows.h>
    #include <NuiApi.h>
    
    INuiSensor *		m_pNuiSensor;
    HANDLE			m_pDepthStreamHandle;
    HRESULT			hr;
    USHORT			arr[640][480];
    
    void Nui_Init(){
    	hr = NuiCreateSensorByIndex(0, &m_pNuiSensor);
    	
    	hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH);
    
    	hr = m_pNuiSensor->NuiImageStreamOpen(
    		NUI_IMAGE_TYPE_DEPTH,
    		NUI_IMAGE_RESOLUTION_640x480,
    		NUI_IMAGE_STREAM_FLAG_ENABLE_NEAR_MODE | NUI_IMAGE_STREAM_FLAG_DISTINCT_OVERFLOW_DEPTH_VALUES,
    		2,
    		NULL,
    		&m_pDepthStreamHandle);
    }
    
    void Nui_Uninit(){
    	m_pNuiSensor->NuiShutdown();
    }
    
    void DepthAlert(){
    	NUI_IMAGE_FRAME imageFrame;
    
    	hr = m_pNuiSensor->NuiImageStreamGetNextFrame(m_pDepthStreamHandle, 0, &imageFrame);
    	
    	if (!FAILED(hr)){
    		INuiFrameTexture * pTexture = imageFrame.pFrameTexture;
    		NUI_LOCKED_RECT LockedRect;
    		pTexture->LockRect(0, &LockedRect, NULL, 0);
    
    		if (LockedRect.Pitch != 0){
    			USHORT * pBufferRun = (USHORT *)LockedRect.pBits;
    			for (unsigned int x=0; x<640; x++){
    				for (unsigned int y=0; y<480; y++){
    					arr[x][y] = *pBufferRun;
    					++pBufferRun;
    				}
    			}
    		}
    		pTexture->UnlockRect(0);
    	}
    	m_pNuiSensor->NuiImageStreamReleaseFrame(NULL, &imageFrame);
    }
    
    int main ()
    {
    	Nui_Init();
    	for (;;){
    		DepthAlert();
    	}
    	Nui_Uninit();
    	system("pause");
        return 0;
    }


    • Edited by duWichtel Monday, April 9, 2012 6:17 PM
    Monday, April 9, 2012 6:16 PM

Answers

  • Here are a few things that might be causing your crash:

    You should pass the stream handle (not NULL) as the first parameter to NuiImageStreamReleaseFrame.

    You should call NuiImageStreamReleaseFrame only if NuiStreamGetNextFrame succeeded (move it up, inside the brace above it).

    Also (this isn't the cause of the crash, but will give you the wrong data):

    The data is stored in the texture row-wise. You should make the outer loop y, and the inner loop x.

    John
    K4W Dev

    • Marked as answer by duWichtel Monday, April 9, 2012 9:00 PM
    Monday, April 9, 2012 8:10 PM

All replies

  • Here are a few things that might be causing your crash:

    You should pass the stream handle (not NULL) as the first parameter to NuiImageStreamReleaseFrame.

    You should call NuiImageStreamReleaseFrame only if NuiStreamGetNextFrame succeeded (move it up, inside the brace above it).

    Also (this isn't the cause of the crash, but will give you the wrong data):

    The data is stored in the texture row-wise. You should make the outer loop y, and the inner loop x.

    John
    K4W Dev

    • Marked as answer by duWichtel Monday, April 9, 2012 9:00 PM
    Monday, April 9, 2012 8:10 PM
  • Hi John,

    thank you very much. :)

    I applied your changes and it's working fine for ~90-110 frames.

    Then the hr of NuiImageStreamGetNextFrame in DepthAlert doesn't return SUCCEEDED. I also get the message in Visual Studio 2010 output a thread ended with Code 0. So restart the m_pDepthStreamHandle every 100 frames? :D

    EDIT: It's working fine after removing usb-cable ... need to fix uninit() ;)

    • Edited by duWichtel Monday, April 9, 2012 8:59 PM
    Monday, April 9, 2012 8:50 PM
  • Keep in mind that in your code, NuiImageStreamGetNextFrame will frequently fail because the next frame isn't ready yet (you're calling with a timeout of zero, and new frames arrive only 30 times per second).

    It's probably preferable to create an event handle and wait on it, rather than running continuously in a busy loop.

    In your Nui_Init:

    hNextFrameEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);

    ...and pass hNextFrameEvent to NuiImageStreamOpen. Then, write your loop as:

    for (;;)
    {
        if (WaitForSingleObject(hNextFrameEvent, INFINITE) == WAIT_OBJECT_0)
        {
            DepthAlert();
        }
        else
        {
            break;
        }
    }
    

    John
    K4W Dev

    Monday, April 9, 2012 10:28 PM