Well, I'm not entirely sure what your function does, but if it opens a connection to a hardware resource (USB?), you should probably close that connection before trying to open it again, using whatever function was supplied. It may be corrupting your memory.
Also, make sure you're using the proper representations for your types, because this can also cause some overwriting.
___________________
Try to take over the world!