I attached the source code file to the original message.
I believe that there is no memory leak there. The issue happens in the 'PreviewThread' method.
What indication do you have of a memory leak. since this can happen very sporadically and after many activations/deactivations of the camera I fail to see a connection to a memory leak.
A memory leak will have the following phenomena:
the longer you run the application with the camera on, the probability that it will happen increases. Am I wrong?
This is not the case hear...
I'm a bit confused.
In the quoted case, the user was successfully using the camera for a while and then their application stopped working. This is indicative of a memory leak.
In your case however, I note that you stated that the application stops after a few seconds. I apologize, I did not see that line the first time I read it. So in your case a memory leak is highly unlikely to be the cause.
It is more likely that you have a USB power stability issue on your PC, with the camera disconnecting when the USB port is not stably supplying the camera with the lower that it needs (the camera is very sensitive to the state of the USB port).
Since the camera functions, albeit for brief periods of time, your drivers and camera firmware are probably okay.
My primary suspicion would be the USB ports, then. Many cases of disconnections have been instantly fixed by plugging the camera into a mains-powered USB hub instead of directly into the PC. These hubs can be bought for around $15 from stores such as Amazon by searching for 'powered usb 3.0 hub'. Others have achieved stability for their camera by using hub-like techniques such as a Macbook external USB-C to HDMI/USB extension.
Surface Pros and Surface Books have a history of problems with USB RealSense cameras, unfortunately. The problems are not universal. The camera has worked for some Surface Book Pro 4 users and not for others.
A few years ago, a user cut open their camera's USB cable and spliced in an external 5v power source. This was how it was originally discovered that powered USB hubs help the camera's stability, whereas unpowered passive hubs may not. Doing so would of course instantly invalidate the camera's warranty though. If you have the ability to create custom builds and are not concerned about the warranty, you could consider splicing a battery connection into the cable that could be turned on and off when needed and would be a suitably mobile solution.
Past cases suggest that there is not another fix that can be suggested, and that the camera will either work with a Surface Book or it will not.
I was one of the people who reported a similar problem about a week ago. MartyG recommended the powered external USB hub option to me, which I tried. In my case it made no difference. My code runs on both SR300 and F200, and I only see the problem on the SR300. The F200 works perfectly. I think if it were a memory leak I would likely see problems on both devices, as the code paths taken through the C++ program are mostly the same. I looked at Snow_'s code (in the link MartyG gave you), and it didn't seem to me that he had a memory leak -- he correctly does a ReleaseFrame after the AcquireFrame, and releases the instance of the sense manager while cleaning up. I also looked at your code and, although I'm not a C# programmer, it appears to me as though you're correctly allocating and freeing resources.
I don't presently have a fix to my problem, but thought it advisable to let you know that the suggestion of a powered external hub did not work for me, before you start slicing up cables on your Surface Pro.