0

I have a video c++ callback function where the parameters get suddently shifted after a few hours. In debug, it will assert on this:

void CCameraInstance::VideoCallback(void *pContext, unsigned char *pBuffer, long nWidth, long nHeight, long nFrameErrorNo)
{
   assert(nWidth < 4000);
   CCameraInstance *pThis = (CCameraInstance*)pContext;
   pThis->PaintFrame(pBuffer, nWidth, nHeight, nFrameErrorNo);
}

When the debugger breaks on the assert, nWidth has a big invalid value. However, nHeight is 320 (the width value) and nFrameErrorNo is 240 (the nHeight value).

How can the parameters get shift that way?

  • You might be seeing the aftershocks of Undefined Behavior. Check the code that executed _before_ this for any array bound violations, buffer overwrite or other such problems. – Masked Man Oct 21 '13 at 15:27
  • Without the full code, I'm just making guesses here. But, I would bet my money that you are passing in an uninitialized value into the width in that function. – Caesar Oct 21 '13 at 15:27
  • Please show the instances where VideoCallback is called. – ChuckCottrill Oct 21 '13 at 15:33
  • I'm still having this same problem after over a year. However, it's hard to reproduce. I'm using Visual Studio 2013. Any debugging tips? – Eric Pronovost Mar 10 '15 at 17:08

1 Answers1

0

The shift could be caused by the hidden this pointer. http://www.learncpp.com/cpp-tutorial/8-8-the-hidden-this-pointer/

From the code you have pasted here

void CCameraInstance::VideoCallback(void *pContext, unsigned char *pBuffer, long nWidth, long nHeight, long nFrameErrorNo)

I can see the callback function is a member of class CCameraInstance

I'm not sure whether you are defining the function as a static function or a normal one. But in theory it should be a static function to avoid the this pointer. Using a C++ class member function as a C callback function

However, I had a problem with C++/CLI even if i have define the member function as static. The this pointer/Handle still exist.

I think you can try to define your function as

void CCameraInstance::VideoCallback(CCameraInstance* test,void *pContext, unsigned char *pBuffer, long nWidth, long nHeight, long nFrameErrorNo)

and have try.

If you are using C++/CLI it would be

void CCameraInstance::VideoCallback(CCameraInstance^ test,void *pContext, unsigned char *pBuffer, long nWidth, long nHeight, long nFrameErrorNo)
Community
  • 1
  • 1