473,406 Members | 2,439 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,406 software developers and data experts.

Delegate still calls class after set to nothing!?!

This is either a bad bug, or I'm not understanding
somthing. In my mind, this should NOT work:

------------------------------------------
Class ShouldntWork
Delegate Sub goHere()

Sub StartHere()
Dim DC as new DeadClass
Dim myDel as New goHere(AddressOf DeadClass.Here)
DC = Nothing
GC.Collect
myDel.Invoke
End Sub
End Class

Class DeadClass
Sub Here
MsgBox "How Did I Get Here?"
End Sub
End Class
--------------------------------------

I'm assuming that since a delegate is really just a
pointer to a place in memory, that invoking it still sends
us there, but isn't this dangerous? Assume that that
location on the heap has been overwritten, who knows what
will happen, right?

Comments, Please!
Nov 20 '05 #1
11 1270
Havent used delegates much, but

Just guessing, as you instantiate a delegate myDel, the removal of the
reference DC to the delegate has no effect.

OHM#

ZorpiedoMan wrote:
This is either a bad bug, or I'm not understanding
somthing. In my mind, this should NOT work:

------------------------------------------
Class ShouldntWork
Delegate Sub goHere()

Sub StartHere()
Dim DC as new DeadClass
Dim myDel as New goHere(AddressOf DeadClass.Here)
DC = Nothing
GC.Collect
myDel.Invoke
End Sub
End Class

Class DeadClass
Sub Here
MsgBox "How Did I Get Here?"
End Sub
End Class
--------------------------------------

I'm assuming that since a delegate is really just a
pointer to a place in memory, that invoking it still sends
us there, but isn't this dangerous? Assume that that
location on the heap has been overwritten, who knows what
will happen, right?

Comments, Please!


Regards - OHM# On**********@BTInternet.com
Nov 20 '05 #2
That's correct, but the place in memory that is being
called should no longer be available... that's what has me
worried.

Nov 20 '05 #3
I think it's surely because of the Garbage Collector running in the back.

Use gc.collect after set to nothing and try again.
<an*******@discussions.microsoft.com> schrieb im Newsbeitrag
news:04****************************@phx.gbl...
That's correct, but the place in memory that is being
called should no longer be available... that's what has me
worried.

Nov 20 '05 #4
After using GC.Collect execute GC.WaitForPendinfFinalizers that will wait
until GC is done collecting..
"ZorpiedoMan" <an*******@discussions.microsoft.com> wrote in message
news:12*****************************@phx.gbl...
This is either a bad bug, or I'm not understanding
somthing. In my mind, this should NOT work:

------------------------------------------
Class ShouldntWork
Delegate Sub goHere()

Sub StartHere()
Dim DC as new DeadClass
Dim myDel as New goHere(AddressOf DeadClass.Here)
DC = Nothing
GC.Collect
myDel.Invoke
End Sub
End Class

Class DeadClass
Sub Here
MsgBox "How Did I Get Here?"
End Sub
End Class
--------------------------------------

I'm assuming that since a delegate is really just a
pointer to a place in memory, that invoking it still sends
us there, but isn't this dangerous? Assume that that
location on the heap has been overwritten, who knows what
will happen, right?

Comments, Please!

Nov 20 '05 #5
Thanks, but that STILL does not answer the question as to
WHY any code executes at all??? The delegate is pointing
to UNUSED memory... there could be ANYTHING at that
point... serious potential crashing possibilities...
Nov 20 '05 #6
"ZorpiedoMan" <an*******@discussions.microsoft.com> schrieb
This is either a bad bug, or I'm not understanding
somthing. In my mind, this should NOT work:

------------------------------------------
Class ShouldntWork
Delegate Sub goHere()

Sub StartHere()
Dim DC as new DeadClass
Dim myDel as New goHere(AddressOf DeadClass.Here)
DC = Nothing
GC.Collect
myDel.Invoke
End Sub
End Class

Class DeadClass
Sub Here
MsgBox "How Did I Get Here?"
End Sub
End Class
--------------------------------------

I'm assuming that since a delegate is really just a
pointer to a place in memory, that invoking it still sends
us there, but isn't this dangerous? Assume that that
location on the heap has been overwritten, who knows what
will happen, right?

Comments, Please!


I also think it should not work: "Here" is not a shared method, so it should
be "...addresof dc.here".

Apart from this, it _should_ work because the delegate still points to the
DC object => there is still a reference to the object => The object is not
collected.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #7
The memory space isn't completely unused just becuase you set it to nothing,
Which is why I recomended GC.WaitforpendingFinalizers because GC.Collete
runs in a different thread. it's sort of like deleting a file you delete it
but the data of the file is still on the drive..
<an*******@discussions.microsoft.com> wrote in message
news:05****************************@phx.gbl...
Thanks, but that STILL does not answer the question as to
WHY any code executes at all??? The delegate is pointing
to UNUSED memory... there could be ANYTHING at that
point... serious potential crashing possibilities...

Nov 20 '05 #8
Hi,

Yes, this is correct behaviour. The delegate stores a reference to both the
target and the method in the target for instance methods. This enables the
GC to move the object in memory, and still reference the same method.
See Delegate class in the framework documents for more info.


"ZorpiedoMan" <an*******@discussions.microsoft.com> wrote in message
news:12*****************************@phx.gbl...
This is either a bad bug, or I'm not understanding
somthing. In my mind, this should NOT work:

------------------------------------------
Class ShouldntWork
Delegate Sub goHere()

Sub StartHere()
Dim DC as new DeadClass
Dim myDel as New goHere(AddressOf DeadClass.Here)
DC = Nothing
GC.Collect
myDel.Invoke
End Sub
End Class

Class DeadClass
Sub Here
MsgBox "How Did I Get Here?"
End Sub
End Class
--------------------------------------

I'm assuming that since a delegate is really just a
pointer to a place in memory, that invoking it still sends
us there, but isn't this dangerous? Assume that that
location on the heap has been overwritten, who knows what
will happen, right?

Comments, Please!

Nov 20 '05 #9
"Bill McCarthy" <bi******@i.primus.com.au> schrieb

Yes, this is correct behaviour. The delegate stores a reference to
both the target and the method in the target for instance methods.
This enables the GC to move the object in memory, and still reference
the same method. See Delegate class in the framework documents for
more info.


That's what I said. Good to know I was right. :-)
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #10
Yep. Shouldn't compile because the addressof should be of the instance, if
that's a typo and he's actually using the instance on the addressof
statemente, then the delegate keeps a reference to the instance - assigning
nothing to the variable DC doesn't change the object it was pointing to -
it's probably not even moved around. In any case, since the original object
still has at least one reference, the garbage collecton won't touch it.

Nov 20 '05 #11
ZorpiedoMan,
I'm assuming that since a delegate is really just a
pointer to a place in memory, that invoking it still sends
us there, but isn't this dangerous? Assume that that
location on the heap has been overwritten, who knows what
will happen, right? You're assuming wrong! As Bill & Armin pointed out: A delegate is a
reference to an object along with a "reference" to a method. For shared
methods, the reference to object happens to be nothing, as obviously its not
needed.

Remember that .NET doesn't really use "a pointer to a place in memory" in
the terms of C++, instead everything is a reference that the runtime knows
about & is able to track! Via the meta data in the assembly. Yes deep under
the covers a reference is "a pointer to a place in memory", however the
important part, is the CLR runtime has a plethora of details on the pointer.

Seeing as the delegate itself still has a reference to the DeadClass object,
the DeadClass object itself will not be garbage collected. If you want the
DeadClass to really go away you will need to set the delegate to nothing
also.

One way to demonstrate this is to put a Finalizer in your DeadClass & call
WaitForPendingFinalizers.

Try single stepping the following program and watch the output window in
VS.NET

Class ShouldntWork

Delegate Sub goHere()

Public Shared Sub Main()
Dim DC As New DeadClass
Dim myDel As New goHere(AddressOf DC.Here)
DC = Nothing

Debug.WriteLine("First Collection")
GC.Collect()
GC.WaitForPendingFinalizers()

myDel.Invoke()

myDel = Nothing
Debug.WriteLine("Second Collection")
GC.Collect()
GC.WaitForPendingFinalizers()

End Sub

End Class

Class DeadClass

Public Sub Here()
Debug.WriteLine("How Did I Get Here?")
End Sub

Protected Overrides Sub Finalize()
MyBase.Finalize()
Debug.WriteLine("Finalize", "DeadClass")
End Sub

End Class

Note the change on the AddressOf statement to reference the instance method
"Here", not the non-existent Shared method "Here".

Hope this helps
Jay

"ZorpiedoMan" <an*******@discussions.microsoft.com> wrote in message
news:12*****************************@phx.gbl... This is either a bad bug, or I'm not understanding
somthing. In my mind, this should NOT work:

------------------------------------------
Class ShouldntWork
Delegate Sub goHere()

Sub StartHere()
Dim DC as new DeadClass
Dim myDel as New goHere(AddressOf DeadClass.Here)
DC = Nothing
GC.Collect
myDel.Invoke
End Sub
End Class

Class DeadClass
Sub Here
MsgBox "How Did I Get Here?"
End Sub
End Class
--------------------------------------

I'm assuming that since a delegate is really just a
pointer to a place in memory, that invoking it still sends
us there, but isn't this dangerous? Assume that that
location on the heap has been overwritten, who knows what
will happen, right?

Comments, Please!

Nov 20 '05 #12

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

Similar topics

13
by: José Joye | last post by:
The example at the end make me perplex... Looking at the msdn documentation, the inheritance tree that relates to "delegate" is as follow: Object<--Delegate<--MulticastDelegate. Unless I'm...
15
by: Sharon | last post by:
I’m trying to build a generic Publisher-Subscriber that will work over the net, so I’m using the Remoting. I wish that the subscriber user will be notify about the messages sent by the...
5
by: Edward Diener | last post by:
The first type to a delegate constructor is obvious. It is a System::Object * . What is the MC++ type of the second argument to the delegate constructor ? In C++ it would be a member function...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
3
by: Stewart | last post by:
Hey Group, Hoping someone can help me out. I have some code which starts up some asynchronous code using a delegate. The code is below. Basically my main code (not shown) calls...
11
by: ryan | last post by:
Hi, I've omitted a large chunk of the code for clarity but the loop below is how I'm calling a delegate function asynchronously. After I start the each call I'm incrementing a counter and then...
11
by: matsi.inc | last post by:
I am looking to make something like a delegate that i can use in my projects but am having a hard time getting started. The behavior I am most interested in is how a delegate changes it's Invoke...
5
by: RP | last post by:
I have a function to fill combo box items. I pass SQL query and combo box name as arguments. This function is located in another class. In my Form, I have declared a delegate to this function...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.