cudaError_enum exception on object deletion

Have a video processing system I’ve been working on for some time, and now adding some capability via FFT’s. Ran into an issue where I would get an execution error, and eventually tracked it down to a cudaMemsetAsync that was writing twice as many bytes as it should have (don’t know how that hasn’t impacted something else to this point…). Now I’m getting another error that’s more confusing.

I have an object that does some processing, and the code runs fine before and after it’s creation. However, if I delete that object during processing (essentially turning that feature off), I see the Visual Studio warning me of the aforementioned exception. Started stripping down the object to see if perhaps I was over writing more memory, but what it boiled down to was the simple allocation for a device pointer. Even though it was never used (the code that used it got commented out), the very allocation of that variable’s memory cause the object to have cuda throw this exception when the object was deleted much later. If I commented that line out, every thing worked fine (of course the underlying algorithms didn’t work 'cause it wasn’t doing its job). And it turns out, that if one of these exceptions are thrown, the CUFFT executions will not work. The FFTs work before that ‘rogue’ object was created, they work fine after it’s created, but start failing the instant the object is deleted (the FFT’s are done in another object and have nothing to do this this object). If I turn off the other feature requiring the FFT, and uncomment all the code I commented out for debugging in the ‘rogue’ object, it runs fine and I can create and destroy that object hundred’s of times with no problem.

At first I though perhaps another function somewhere else was writing into that guy’s space, but several other allocations happen after the one the causes the problems, testing shows whether they were allocated or not didn’t cause the exception to be thrown. As a backup, ran cuda-memcheck on the application, but it showed zero errors.

Looked at several threads on these forums but most are quite old, or discuss code that can’t run. My code (minus the newly added FFT part) seems to run fine with the errors. I also get exceptions thrown when running the FFT plans, but that doesn’t stop them from executing until this object gets deleted.

Any comments about what can cause this exception to get thrown on destruction of an object that uses device memory allocations? On many occasions I’ve done the double free and free empties, but this is something else.

Don’t know what caused this issue, but found a work around. It turns out the allocation I described above was actually an container for 2D array (not cudaArray, just memory used for processing 2D images) which holds the pointer to the device memory, it’s pitch and dimensions. Originally, I declared this as a member variable since it was always needed, and would simply be destroyed on loss of scope (object destruction). I decided to take matters into my own hands and make the local variable a pointer to this container type instead of the container itself. Granted, now I have to handle the container’s destruction in its owner’s destruction method, but that’s easy. It turns out this works without throwing an exception.

Still unsure why this work around was needed…

Alright, I’m an idiot. Digging deeper into the container class I discovered that the destructor wasn’t setting the data pointer member to zero, hence if I ‘cleared’ it, and then the destructor ‘destroyed’ it, it would attempt to cudaFree a pointer that was already free’d but not set to NULL. My bad.