473,383 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

What happens if exception is thown within a fixed block?

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
8 2270
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
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
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
"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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and there is something that I want to ask you about. It says "since the constructor does not return any value, the only way to handle errors that occur...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
6
by: Chris Newcombe | last post by:
Please could someone on the VC++ 7.0 compiler team (note; not 7.1) tell me if this code is handled 'correctly' (i.e. as the original poster suggests) in all cases? ...
1
by: Noor | last post by:
Hi all, I am trying to catch all types of exceptions from a app regardless of whether it is in debugger mode( VS development environment) or run the.exe file outside the IDE. My App...
24
by: Dave | last post by:
Maybe I'm missing something here, but I can't see the purpose of the 'finally' keyword. What is the difference between: try { doSomething() } catch { handleError(); }
4
by: Rob Richardson | last post by:
Greetings! I am working on an application that targets a Pocket PC running Windows CE and SQL Server CE. Almost all functions in the application use a Try block with a Catch block that looks...
4
by: garyusenet | last post by:
Hi I'm using the following code which is finally working. Public Class Form1 Shared ActElement As Object Shared ActFields As DataSet Public Sub SetActElement() Dim objApp As New Object
9
by: TC | last post by:
Hey All, I posted this to the Crypto users group and forgot to add the VB.Net users group. I apologize for any confusion. I have been testing a try / catch / finally block and purposely...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.