RenderSurface.SetViewport crashes after Rendertarget was resized
The following code change to the C# example "RenderToTexture.cs" generates an AccessViolationException:
// old code:
...
RenderSurface surface = renderTexture.RenderSurface;
Viewport rttViewport = new Viewport(Context, rttScene, rttCameraNode.GetComponent<Camera>(), null);
surface.SetViewport(0, rttViewport);
// insert this code:
renderTexture.SetSize(640, 480, Graphics.RGBFormat, TextureUsage.Rendertarget);
// renderTexture.RenderSurface was recreated by renderTexture.SetSize
renderTexture.RenderSurface.SetViewport(0, rttViewport);
However, applying the same change to the unmanaged C++ example "RenderToTexture.cpp" does not generate an exception and works as it should, so this is clearly a wrapper issue:
// old code:
...
RenderSurface* surface = renderTexture->GetRenderSurface();
SharedPtr<Viewport> rttViewport(new Viewport(context_, rttScene_, rttCameraNode_->GetComponent<Camera>()));
surface->SetViewport(0, rttViewport);
// insert this code:
renderTexture->SetSize(640, 480, Graphics::GetRGBFormat(), TEXTURE_RENDERTARGET);
renderTexture->GetRenderSurface()->SetViewport(0, rttViewport);
Exception details:
System.AccessViolationException was unhandled
HResult=-2147467261
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=Urho
StackTrace:
at Urho.RenderSurface.RenderSurface_SetViewport(IntPtr handle, UInt32 index, IntPtr viewport)
at Urho.Samples.RenderToTexture.CreateScene() in D:\dev\_repos\urho\Samples\FeatureSamples\Core\10_RenderToTexture\RenderToTexture.cs:line 183
at Urho.Samples.RenderToTexture.Start() in D:\dev\_repos\urho\Samples\FeatureSamples\Core\10_RenderToTexture\RenderToTexture.cs:line 38
at Urho.Application.ProxyStart(IntPtr h)
at Urho.Application.Application_Run(IntPtr handle)
at Urho.Samples.Mac.Program.Main(String[] args) in D:\dev\_repos\urho\Samples\FeatureSamples\Console\Program.cs:line 23
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
I tried to debug this myself but the debugger would behave totally strange, the call stack would claim that the code line
viewports_[index] = viewport;
in RenderSurface.cpp would directly call
Urho3D::SharedPtr<Uhro3D::Node>::operator=
even though Viewport and Node are unrelated.
This was tested with the NuGet package versions 1.5.22 (== commit 6544107b2e3f119bd5e6148da3dc87f4d2be3994) and package version 1.5.2
Looks like https://github.com/xamarin/urho/issues/302