473,763 Members | 4,584 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2288
ms news group <no***@noret.co mwrote:
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.co m>
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.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
ms news group <no***@noret.co mwrote:
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.co m>
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.co mwrote:
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.co m>
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.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
ms news group <no***@noret.co mwrote:
>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.bewro te in message
news:2E******** *************** ***********@mic rosoft.com...
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
ms news group <no***@noret.co mwrote:
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.co mwrote in message
news:u6******** ******@TK2MSFTN GP03.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.bewro te in message
news:4E******** *************** ***********@mic rosoft.com...
"ms news group" <no***@noret.co mwrote in message
news:u6******** ******@TK2MSFTN GP03.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.co mwrote:
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.co m>
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
6007
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
2798
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 during its execution is to use exception handling. You can deal with errors that occur during the execution of the constructor's body by using try block
44
4226
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 user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box, etc.). 2. Low-level operations that are used to carry out the high level tasks
6
1676
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? http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=89ed5u%248ck%40library1.airnews.net (Incase the URL doesn't make it I've copied it below.) I have a situation where I really need this technique. But the fact that Dave Abrahams wasn't sure about it very much worries...
1
1973
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 contains the thousand of classes and I do not want to use the Try Catch block in each class. Because it is logically similar to GOTO statement. I want to prevent exception from being swallowed.
24
2174
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
2040
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 like this: Try TryToDoIt() Catch e as Exception LogTheError(e)
4
4663
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
1956
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 raising exceptions and I've noticed that if an exception of "Length of the data to decrypt is invalid." is raised with the CryptoStream object, later this exception will get raised a second time and thrown to the caller when trying to close the stream...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.