Thread: memory usage keeps going up when retrieving webcam image

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    memory usage keeps going up when retrieving webcam image

    I've written a JAva program that uses JNI to call some C++ methods to retrieve webcam images using video for windows. The problem is that every time I retrieve an image, memory usage keeps going up. I'm still a noob to C++ so the problem may be something really simple. Here's my code:

    Code:
    HWND m_capWindow;
    int connectedIndex;
    LRESULT PASCAL callbackProc(HWND hWnd, LPVIDEOHDR lpVHdr);
    LRESULT PASCAL callbackProc(HWND hWnd, LPVIDEOHDR lpVHdr)
    {
    	return 0;
    }
    
    JNIEXPORT void JNICALL Java_client_desktop_dateclick_Test_connect
      (JNIEnv *, jobject, jint index) {
    		m_capWindow = capCreateCaptureWindow ("", WS_CHILD,	0, 0, 0, 0, GetDesktopWindow(), 10);
    		//m_capWindow = capCreateCaptureWindow ("Capture", WS_CHILD | WS_VISIBLE,	50, 50, 480, 360, GetDesktopWindow(), 10);
    
    		// connect to the new driver
    		BOOL rval = capDriverConnect (m_capWindow, index);
    		capSetCallbackOnFrame(m_capWindow, callbackProc);
    		capPreview(m_capWindow, FALSE);
    }
    
    
    
    
    JNIEXPORT jbyteArray JNICALL Java_client_desktop_dateclick_Test_getImage
      (JNIEnv *env, jobject) {
    
    	CString fileName;
    	int contentLength;
    	BYTE *frame;
    
        capGrabFrame(m_capWindow);
        //capGrabFrameNoStop(m_capWindow);
        fileName = "capframe.bmp"; //and use this file...
        capFileSaveDIB( m_capWindow, (LPCTSTR)fileName);
    
        CFile jpegTemp;
        // now read the resulting file into a buffer
        jpegTemp.Open("capframe.bmp", CFile::modeRead);
        contentLength = (UINT)jpegTemp.GetLength();
        //allocate the buffer for the jpeg frame
        frame = new BYTE[contentLength];
        //now read in the jpeg file
        jpegTemp.Read(frame, contentLength);
        jpegTemp.Close();
    
    	jbyteArray ba = env->NewByteArray(contentLength);
    	env->SetByteArrayRegion(ba, 0, contentLength, (jbyte*) frame); 
    
    	delete frame
    
    	return ba;
    }
    
    JNIEXPORT void JNICALL Java_client_desktop_dateclick_Test_disconnect
      (JNIEnv *env, jobject) {
    			capDriverDisconnect(m_capWindow);
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    One thing I notice is that you are using the wrong form of delete. Since you used new[] to allocate space for the BYTE array, you should use delete [] frame to delete it. (There is a missing semi-colon there in your code, how does that even compile?)

    You could also just use a vector<BYTE> so you don't have to worry about managing the memory.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    2
    awesome

    delete [] frame;

    works now. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying memory, pointers and the like.
    By psychopath in forum C++ Programming
    Replies: 34
    Last Post: 12-12-2006, 01:37 PM
  2. Array Fundamentals: memory usage
    By 3saul in forum C Programming
    Replies: 2
    Last Post: 02-06-2006, 12:32 PM
  3. [C] Trouble with memory usage
    By BianConiglio in forum Windows Programming
    Replies: 1
    Last Post: 09-28-2004, 08:57 AM
  4. Memory Allocation in Intell Image processing liberary
    By nisar in forum Windows Programming
    Replies: 0
    Last Post: 01-12-2003, 07:29 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM