473,511 Members | 13,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Do local references destruct on method exit?

(Sorry, I accidentally posted this to
microsoft.public.dotnet.csharp.general, and I didn't mean to!)

In the following method, a byte array is made, and then passed to
Socket.BeginReceive. The byte array is local. But, presumably,
Socket.BeginReceive creates a reference to it, so the byte array (a
reference) does disappear, but the object the reference 'points' to
(the array data) is referenced by something inside of
Socket.BeginReceive, so all is well when the data receive call back
(DataReceive_CallBack in this case) is called. Right?

public void MyMethod()
{
byte[] bytesArray = new byte[1024];
mySocket.BeginReceive(bytesArray, 0, bytesArray.Length,
SocketFlags.None, DataReceive_CallBack, bytesArray);
}

Zytan

Feb 27 '07 #1
4 1500
>But, presumably,
Socket.BeginReceive creates a reference to it, so the byte array (a
reference) does disappear, but the object the reference 'points' to
(the array data) is referenced by something inside of
Socket.BeginReceive, so all is well when the data receive call back
(DataReceive_CallBack in this case) is called. Right?
You're right that you don't have to worry about the array object being
garbage collected as long as it's in use.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 27 '07 #2
Zytan <zy**********@yahoo.comwrote:
(Sorry, I accidentally posted this to
microsoft.public.dotnet.csharp.general, and I didn't mean to!)

In the following method, a byte array is made, and then passed to
Socket.BeginReceive. The byte array is local. But, presumably,
Socket.BeginReceive creates a reference to it, so the byte array (a
reference) does disappear, but the object the reference 'points' to
(the array data) is referenced by something inside of
Socket.BeginReceive, so all is well when the data receive call back
(DataReceive_CallBack in this case) is called. Right?
No. The byte array doesn't disappear. The variable "bytesArray"
disappears, but the value of that variable was just a reference. The
byte array itself is on the heap, and isn't local at all.

--
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
Feb 27 '07 #3
You're right that you don't have to worry about the array object being
garbage collected as long as it's in use.
Yes, Mattias, thanks. I just tested and confirmed this.

Zytan

Feb 27 '07 #4
No. The byte array doesn't disappear. The variable "bytesArray"
disappears, but the value of that variable was just a reference. The
byte array itself is on the heap, and isn't local at all.
Hi Jon, yes, exactly. It was your page on parameter passing that
makes it so clear (and also with the addendum with pictures) that
shows why this is the case, but I just had to be sure. I just
finished testing it to be positive.

Thanks

Zytan
Feb 27 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3619
by: Robert Tarantino | last post by:
Hello, I am trying to find a way to create a scheduled task or service that will copy my local profile folders under "Documents and settings" to a network drive. This would allow me to restore...
6
4636
by: Lasse Skyum | last post by:
I'm currently learning STL and I hate not knowing what is gooing on "inside" STL... not because I really _need_ to know it to develop my game project, but that's just my nature... like most of you...
21
4675
by: Steve | last post by:
Does anyone have any ideas on how to write self-destruct code? I need to run a one-time procedure the next time a database opens. If the procedure is successful, the procedure needs to be deleted...
6
3612
by: Brad | last post by:
I have a win2003 server workstation with multiple webs, each web has it's own ip address. In VS2005, if I select to open an existing web site, select Local IIS, the dialog correctly displays a...
5
2423
by: Michal Kwiatkowski | last post by:
Hi! I'm building a class that most of methods have similar intro, something like this: def method(self): var_one = self.attr_one var_two = self.attr_two.another_attr empty_list =
5
3323
by: Frederick Gotham | last post by:
If we have a simple class such as follows: #include <string> struct MyStruct { std::string member; MyStruct(unsigned const i) {
55
6151
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
0
1269
by: Chris Thomasson | last post by:
I am fine-tuning some last minute things in my c++ allocator lib and was thinking of ways to manage local references to system objects created with factory functions. I started to tinker around...
28
2529
by: cpluslearn | last post by:
Hi, I have a local class inside a function template. I am able to wrap any type in my local class. Is it legal C++? What type of class is Local? Is it a class template or regular class? Thanks...
0
7144
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...
0
7356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7427
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...
1
7085
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...
0
3227
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...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1577
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 ...
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.