1

I'm a newbe to C++ and started fooling around with my Kinect V2 and libfreenect2. The building of the library worked fine for me, I guess. But when I try to use it in my code I get some weird exceptions.

Freenect2 freenect2;
std::string serial="";
Freenect2Device *dev = 0;

freenect2.enumerateDevices();
serial = freenect2.getDefaultDeviceSerialNumber();
dev = freenect2.openDevice(serial);

Everytime I run this code, it fails at getting the serial number [serial = freenect2.getDefaultDeviceSerialNumber();] with:

"Unhandled exception thrown: read access violation.

_Pnext was 0xFFFFFFFFFFFFFFFF."

In case I'm using

Freenect2 freenect2;
Freenect2Device *dev = 0;
SyncMultiFrameListener listener(Frame::Color);
FrameMap frames;

freenect2.enumerateDevices();
dev = freenect2.openDefaultDevice();
dev->setColorFrameListener(&listener);
dev->start();

listener.waitForNewFrame(frames)

instead, it fails while waiting for a new frame [listener.waitForNewFrame(frames)] with following exception thrown:

" Exception thrown at 0x000000018026D0C2 (ig75icd64.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation writing location 0x000000002BA0D700. "

So, what am I doing wrong and how can I do it better?

Thanks for your advice.

Edit: shortened the code...

  • 1
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Oct 13 '16 at 16:30
  • `0xC0000005` hints about a uninitalized pointer was dereferenced. – πάντα ῥεῖ Oct 13 '16 at 16:31
  • Well I'm not new to programming though. I know how to use a debugger. And yeah, there is something messed up with a pointer, I guess. But it does not happen in my code. It happens in some included files. That's why I'm asking this question here... – user3014569 Oct 13 '16 at 16:36
  • So nice to know you know how to use the debugger. Did you check the result of this statement `dev = freenect2.openDefaultDevice();` while stepping through your code? As mentioned post a [MCVE] please. Otherwise it's unlikely any one will be able to diagnose your problem. – πάντα ῥεῖ Oct 13 '16 at 16:43
  • Yes, I did. In the first code snippet it fails at getting the serial number before even filling 'dev' with data. In the second snippet 'dev' is filled with, I guess, correct data. – user3014569 Oct 13 '16 at 16:50
  • Well, it is the most minimalistic and yet complete example of the problem I guess... or what is missing in your eyes? dev contains: - dev 0x000000c4d0846e70 {state_=Open (1) has_usb_interfaces_=true context_=0x000000c4c56b66a0 {managed_usb_context_=...} ...} libfreenect2::Freenect2Device * {freenect2.dll!libfreenect2::Freenect2DeviceImpl} – user3014569 Oct 13 '16 at 16:55
  • You should read the linked article about the MCVE. – πάντα ῥεῖ Oct 13 '16 at 16:57
  • Well, I shortened the code to a really minimalistic form now... I hope it made it more readable. – user3014569 Oct 13 '16 at 17:05

1 Answers1

0

I was facing the same problem on Debug mode but it worked on Release.

After some hours researching, I've managed to solve the problem through a compilation flag on the libfreenect2 project (on debug setting).

The freenect2 project was marked as Multi-threaded DLL (/MD) and if you change it to Multi-threaded Debug DLL (/MDd) it should work.

In VS2015 you can access this property on:

Project Properties -> C/C++ -> Code Generation ->Runtime Library

mariopinto
  • 33
  • 7