PC Review


Reply
Thread Tools Rate Thread

Delegate still calls class after set to nothing!?!

 
 
ZorpiedoMan
Guest
Posts: n/a
 
      11th Dec 2003
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!
 
Reply With Quote
 
 
 
 
One Handed Man [ OHM# ]
Guest
Posts: n/a
 
      11th Dec 2003
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# (E-Mail Removed)


 
Reply With Quote
 
Guest
Posts: n/a
 
      11th Dec 2003
That's correct, but the place in memory that is being
called should no longer be available... that's what has me
worried.

 
Reply With Quote
 
Captain Chaos
Guest
Posts: n/a
 
      11th Dec 2003
I think it's surely because of the Garbage Collector running in the back.

Use gc.collect after set to nothing and try again.
<(E-Mail Removed)> schrieb im Newsbeitrag
news:04f001c3c024$2e4e1290$(E-Mail Removed)...
> That's correct, but the place in memory that is being
> called should no longer be available... that's what has me
> worried.
>



 
Reply With Quote
 
Mike Bulava
Guest
Posts: n/a
 
      11th Dec 2003
After using GC.Collect execute GC.WaitForPendinfFinalizers that will wait
until GC is done collecting..


"ZorpiedoMan" <(E-Mail Removed)> wrote in message
news:1234301c3c01f$c2adb8f0$(E-Mail Removed)...
> 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!



 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      11th Dec 2003
"ZorpiedoMan" <(E-Mail Removed)> 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

 
Reply With Quote
 
Guest
Posts: n/a
 
      11th Dec 2003
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...
 
Reply With Quote
 
Mike Bulava
Guest
Posts: n/a
 
      11th Dec 2003
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..


<(E-Mail Removed)> wrote in message
news:058e01c3c02b$65141ed0$(E-Mail Removed)...
> 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...



 
Reply With Quote
 
Bill McCarthy
Guest
Posts: n/a
 
      12th Dec 2003
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" <(E-Mail Removed)> wrote in message
news:1234301c3c01f$c2adb8f0$(E-Mail Removed)...
> 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!



 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      12th Dec 2003
"Bill McCarthy" <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delegate for event in class tshad Microsoft C# .NET 3 8th Mar 2010 04:38 PM
hook all function calls to a class Wazza Microsoft C# .NET 2 18th Nov 2009 03:54 AM
best way to wait for async delegate calls to complete ryan Microsoft VB .NET 11 21st Oct 2005 06:04 PM
Class design - minimize DB calls H Branyan Microsoft ASP .NET 1 6th Nov 2003 08:38 PM
Get the form which calls some code in a class??? Alex Stevens Microsoft VB .NET 2 16th Sep 2003 02:05 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:07 AM.