By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,320 Members | 2,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,320 IT Pros & Developers. It's quick & easy.

What happens if exception is thown within a fixed block?

P: n/a
What happens if exception is thown within a fixed block? Will the pinned
memory buffer get unpinned? and if the pinning pointer points to a managed
memery buffer allocated within the throwing function, will the memory buffer
get garbage-collected finally?

I check the language spec, also searched the internet, found nothing
regarding this question.
May 17 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
ms news group <no***@noret.comwrote:
What happens if exception is thown within a fixed block? Will the pinned
memory buffer get unpinned? and if the pinning pointer points to a managed
memery buffer allocated within the throwing function, will the memory buffer
get garbage-collected finally?

I check the language spec, also searched the internet, found nothing
regarding this question.
Which memory buffer do you mean? Could you given an example? As far as
I'm aware, the fixed statement just makes sure that during the course
of the block, the variable's location in memory is not changed and the
object referred to (where appropriate) is not garbage collected.

If a managed memory buffer is allocated, it will be subject to garbage
collection when it's not pinned any more - that needn't happen as soon
as the fixed statement finishes.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 17 '07 #2

P: n/a
Thanks Jon.

Let's have simple example here:

void dd()
{
byte [] buffer = new byte[1]

(fixed byte * pb = &buffer)
{
if( sometest())
goto outofblock;
else
throw new exception();
}

outofblock:

}

For the code above, I am only sure that the "goto" statement will unpin the
pb, but will the "throw" also do unpin?
In other words, is this "fixed" block similar to a "lock" block, in that a
try/finally block is actually in place to release the "pinning" or
"locking"?

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
ms news group <no***@noret.comwrote:
What happens if exception is thown within a fixed block? Will the pinned
memory buffer get unpinned? and if the pinning pointer points to a
managed
memery buffer allocated within the throwing function, will the memory
buffer
get garbage-collected finally?

I check the language spec, also searched the internet, found nothing
regarding this question.

Which memory buffer do you mean? Could you given an example? As far as
I'm aware, the fixed statement just makes sure that during the course
of the block, the variable's location in memory is not changed and the
object referred to (where appropriate) is not garbage collected.

If a managed memory buffer is allocated, it will be subject to garbage
collection when it's not pinned any more - that needn't happen as soon
as the fixed statement finishes.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

May 18 '07 #3

P: n/a
ms news group <no***@noret.comwrote:
Let's have simple example here:
<snip>
For the code above, I am only sure that the "goto" statement will unpin the
pb, but will the "throw" also do unpin?
Yes.
In other words, is this "fixed" block similar to a "lock" block, in that a
try/finally block is actually in place to release the "pinning" or
"locking"?
I don't think it works quite like that. I *think* the GC just looks at
where a thread's current execution point is and works out whether or
not it's in a fixed region when it considers whether or not it can move
things.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 18 '07 #4

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
ms news group <no***@noret.comwrote:
>Let's have simple example here:

<snip>
>For the code above, I am only sure that the "goto" statement will unpin
the
pb, but will the "throw" also do unpin?

Yes.
>In other words, is this "fixed" block similar to a "lock" block, in that
a
try/finally block is actually in place to release the "pinning" or
"locking"?

I don't think it works quite like that. I *think* the GC just looks at
where a thread's current execution point is and works out whether or
not it's in a fixed region when it considers whether or not it can move
things.
The GC doesn't have to do that, when the fixed block is entered, the object
is registered in the handle table as "pinned", when the scope is left (say
when an exception is thrown) the pinned handle gets removed from the handle
table, so, when the GC comes along he's free to move the object.

Willy.

May 18 '07 #5

P: n/a
Thanks.

I dont really mean that a try/finally mechanism is behind the scene. I am
only concerned with the question whether the pinned block get unpinned
regardless how excecution flow leave the block, be it normal or unnormal.
From what you told me, it seems so, But anyway, I think this is a
legitimite question, and MS perhaps should make this point clear in its doc
or spec.

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:2E**********************************@microsof t.com...
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
ms news group <no***@noret.comwrote:
Let's have simple example here:
<snip>
For the code above, I am only sure that the "goto" statement will unpin
the
pb, but will the "throw" also do unpin?
Yes.
In other words, is this "fixed" block similar to a "lock" block, in
that
a
try/finally block is actually in place to release the "pinning" or
"locking"?
I don't think it works quite like that. I *think* the GC just looks at
where a thread's current execution point is and works out whether or
not it's in a fixed region when it considers whether or not it can move
things.

The GC doesn't have to do that, when the fixed block is entered, the
object
is registered in the handle table as "pinned", when the scope is left (say
when an exception is thrown) the pinned handle gets removed from the
handle
table, so, when the GC comes along he's free to move the object.

Willy.

May 18 '07 #6

P: n/a
"ms news group" <no***@noret.comwrote in message
news:u6**************@TK2MSFTNGP03.phx.gbl...
Thanks.

I dont really mean that a try/finally mechanism is behind the scene. I am
only concerned with the question whether the pinned block get unpinned
regardless how excecution flow leave the block, be it normal or unnormal.
From what you told me, it seems so, But anyway, I think this is a
legitimite question, and MS perhaps should make this point clear in its
doc
or spec.
The ECMA-334 "C# Language Specification" is quite clear about this, from :

27.6 The fixed statement

....
For each address computed by a fixed-pointer-initializer the fixed statement
ensures that the variable
referenced by the address is not subject to relocation or disposal by the
garbage collector for the duration of
the fixed statement.
....

Willy.

May 18 '07 #7

P: n/a
hehe,
Does this "For each address computed by a fixed-pointer-initializer the
fixed statement
ensures that the variable referenced by the address is not subject to
relocation or disposal by the
garbage collector for the duration of the fixed statement."
directly leads to "also ensure that the variable get unpinned after leaving
the scope, either normally, or unnormally in the case an exception is thronw
in the scope."?


"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:4E**********************************@microsof t.com...
"ms news group" <no***@noret.comwrote in message
news:u6**************@TK2MSFTNGP03.phx.gbl...
Thanks.

I dont really mean that a try/finally mechanism is behind the scene. I
am
only concerned with the question whether the pinned block get unpinned
regardless how excecution flow leave the block, be it normal or
unnormal.
From what you told me, it seems so, But anyway, I think this is a
legitimite question, and MS perhaps should make this point clear in its
doc
or spec.

The ECMA-334 "C# Language Specification" is quite clear about this, from :

27.6 The fixed statement

...
For each address computed by a fixed-pointer-initializer the fixed
statement
ensures that the variable
referenced by the address is not subject to relocation or disposal by the
garbage collector for the duration of
the fixed statement.
...

Willy.

May 20 '07 #8

P: n/a
ms news group <no***@noret.comwrote:
Does this "For each address computed by a fixed-pointer-initializer the
fixed statement
ensures that the variable referenced by the address is not subject to
relocation or disposal by the
garbage collector for the duration of the fixed statement."
directly leads to "also ensure that the variable get unpinned after leaving
the scope, either normally, or unnormally in the case an exception is thronw
in the scope."?
Well, it certainly doesn't imply that the "fixedness" has any impact
outside the duration of the fixed statement.

It's not a direct proof, but I'd say it's enough to be pretty
confident.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 20 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.