zengqh / slimdx

Automatically exported from code.google.com/p/slimdx
0 stars 0 forks source link

Object ownership issue with D3D9.Device.GetRenderTarget. #340

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Using revision 734.

Consider following code:
Surface oldTarget = device.GetRenderTarget(0);
device.SetRenderTarget(0, newTarget);
// some rendering
device.SetRenderTarget(0, oldTarget);
oldTarget.Dispose(); // <-- PROBLEM HERE

The last line is quite problematic because it depends whether oldTarget is
default target provided by Direct3D or was created by me and set as
current. In the latter case I'm disposing surface which is used somewhere
else and the program crashes. On the other hand if I don't call dispose and
surface comes from Direct3D I get leak. I can use Tag property to mark
where surface came from but it looks like a hack to me. ;)

Is this expected behaviour?

Original issue reported on code.google.com by luke.pat...@gmail.com on 20 Sep 2008 at 1:13

GoogleCodeExporter commented 9 years ago
It's a problematic but known edge case -- I'm actually in the process of 
documenting 
it right now:
http://code.google.com/p/slimdx/source/browse/trunk/docs/DocSite/Help/Topics/Obj
ect%
20Lifetimes.aml

It was a danger we were aware of going into implementing the new object 
lifetime 
model. Basically, it's the least sucky alternative. My recommendation is that, 
if 
you can, always get the render target up front, and dispose it just once when 
appropriate. (So this way, even the default render target has a SlimDX object 
associated with it.) Avoid the behavior where you get it and conditionally 
dispose 
it there and then. The whole point of this was that SlimDX won't keep 
increasing the 
ref count on the object if you call that method multiple times, so it's okay to 
continually call it frame after frame as long as you remember to Dispose it 
just the 
once.

No, it's not ideal, but we felt that this was the least common and easiest 
problem 
case to handle, given the alternatives and given SlimDX's leak debugging 
support. 
Let me know if there's anything else you need.

Original comment by promit....@gmail.com on 20 Sep 2008 at 3:20

GoogleCodeExporter commented 9 years ago
Getting target up front is a good idea. But couldn't it be done automatically 
by SlimDX?

Original comment by luke.pat...@gmail.com on 21 Sep 2008 at 2:44

GoogleCodeExporter commented 9 years ago
That would open up a whole range of new problems about ownership and lifetime 
management.

Original comment by promit....@gmail.com on 21 Sep 2008 at 4:48