FacticiusVir / SharpVk

C# Bindings for the Vulkan API & SPIR-V
MIT License
147 stars 18 forks source link

Premature gabrage collection of DebugReportCallback delegate #44

Open SinnersSumit opened 6 years ago

SinnersSumit commented 6 years ago

I've found a rather odd bug in which the delegate used for DebugReportCallback is garbage collected, thus causing a random crash when the native handler calls it during the next vulkan call. Fortunately, I believe this should be easily fixed by simply adding the delegate as a member to DebugReportCallback My temporary solution was to simply preserve the delegate by keeping a reference to it bound to my window. It works well enough.

FacticiusVir commented 6 years ago

You'll see in the samples that your workaround is the approach I've taken so far - hold a reference to the delegate in the calling code because the unmanaged function pointer passed to Vulkan isn't tracked by the garbage collector. Adding a reference to the DebugReportCallback handle adds some complexity to the generator, as it'll need to have a concept of reference lifetimes to understand which passed-in references are now owned by the returned/created handle, and I need to check where else in the API are callbacks being used this way.

I'll likely add this as part of the rework I'm doing for Vulkan 1.1 - I'm going to have to add more scope for manual overrides to the generator, and I expect this change will fit in there.