>> Just thinking out loud, but might it have something to do
>> with threading concerns? If I hand out a reference to
>> something on my stack to a strange API how do I know that
>> it will finish with it before my stack changes..
>
>But it isn't a strange API. After all, presumably the Managed DirectX
>API designers understand how the DirectX API works. I'm no DirectX
>programmer, but I assume that since it isn't documented, that the API
>will not hold on the pointer.

I guess I meant 'strange' as in 'closed source, poorly documented, "I
didn't write it", etc.' (don't know much about DirectX doco, so that's
not a sledge as such).

Perhaps the API designers are just trying to save programmers from
themselves again..?

Say for example I'm targeting the managed DirectX API, and my client
code is multithreaded, and I do something horrible, passing structs
allocated on my stack by ref to a 'helper' method that dereferences them
and passes them by value to the underlying API for execution on another
thread. I'd still be safe, because the dereferencing happened on my
thread, however if I passed them by ref to the other thread, then by the
time the second thread is executing my original thread has changed
state, and literally anything might be on the stack.

Is there any safeguard in place that could catch this type of thing? As
I (vaguely) understand it, the difference between a class and struct is
how much 'support' they get from the CLR. So a struct is more like a
'set of bits' and a class is more like a 'component' (it gets an object
id, etc.). If you're just passing references around to a 'set of bits'
what metadata do you have that says what is *really* at that set of
bits..?

I don't know a lot about the details, but it seems like this is why they
may have decided to pass by value. It's just safer, and there is less to
think about.

The other argument might have been that the value is 'read' many more
times that it is 'written', so the extra level of indirection (to
dereference the pointer) ends up costing more than just passing a few
extra bits up front. This argument seems to be shot to bits by the fact
that the real work gets done on a pointer though..

John.

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com
Some .NET courses you may be interested in:

NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to