473,804 Members | 3,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What has managed code achieved?

Hi

What are the advantages actually achieved of managed code? I am not talking
of theory but in reality.

Thanks

Regards
Oct 20 '08
66 3381
On Fri, 24 Oct 2008 14:01:58 -0500, "Alex Clark" wrote:
>Just to clarify, are you saying that in VB6 if you didn't de-reference an
object before it went out of scope, it would automatically be cleaned up
anyway? I'm not saying this in an accusatory manner, but everything I ever
read in terms of good practice and even articles on MSDN seemed to imply
otherwise - in fact I can even remember it being touted as a selling point
to upgrade to VB.NET.

Example (VB6):

Private Sub Test()

Dim objRef As SomeClass
Set objRef = New SomeClass
' ...
' ...
Set objRef = Nothing ' <--- Dereferencing before it goes out of scope

End Sub

Are you saying that the last line, "Set objRef = Nothing", was therefore
unneccesary in VB6?
I think that this is what Ben is saying, and I can confirm that it is
absolutely true. The above "Set objRef = Nothing" is like setting a
string to vbNullString before leaving a sub/function ... it frees
memory but that would happen automatically anyway.

Apart from API calls handled improperly, the only way to produce those
memory leaks that VB is allegedly well known for is to have unresolved
circular object references.
>[...] then apparently VB6 was a bit smarter than I gave it credit for!
Hehe! :-)

Wolfgang
Oct 24 '08 #61
"Alex Clark" <qu****@noemail .noemailschrieb :
Just to clarify, are you saying that in VB6 if you didn't de-reference an
object before it went out of scope, it would automatically be cleaned up
anyway? I'm not saying this in an accusatory manner, but everything I
ever read in terms of good practice and even articles on MSDN seemed to
imply otherwise - in fact I can even remember it being touted as a selling
point to upgrade to VB.NET.

Example (VB6):

Private Sub Test()

Dim objRef As SomeClass
Set objRef = New SomeClass
' ...
' ...
Set objRef = Nothing ' <--- Dereferencing before it goes out of scope

End Sub

Are you saying that the last line, "Set objRef = Nothing", was therefore
unneccesary in VB6? I was always under the impression that memory leaks
courtesy of objects that are still referenced (but out of scope) would
occur if you did *not* de-reference before they went out of scope
No, the reference stored in the local variable would have been removed
automatically. However, there were some pitfalls: Jumping out of a 'With'
block, for example, led to a reference which could not be removed but
prevented the object from being destroyed:

\\\
Private Declare Sub CopyMemory _
Lib "Kernel32.d ll" _
Alias "RtlMoveMem ory" _
( _
ByRef Destination As Any, _
ByRef Source As Any, _
ByVal Length As Long _
)

Private Sub Form_Click()
Dim f As Form1
Set f = New Form1
Call MsgBox(GetRefCo unt(f)) ' 1.

' Increments reference count.
With f

' Jumping out of the block does not decrement the
' reference count.
GoTo Label

' Decrements reference count.
End With
Label:
Call MsgBox(GetRefCo unt(f)) ' 2.
End Sub

Private Function GetRefCount(ByR ef Object As IUnknown) As Long
Call CopyMemory(GetR efCount, ByVal ObjPtr(Object) + 4, 4)
GetRefCount = GetRefCount - 2
End Function
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Oct 25 '08 #62
Ok, let me than complete what you did not say.

An object is removed as soon as it has no existing reference anymore itself,
or that its reference is used in another object (not being the object
itself).

It can have a reference in a Module or the Class is Shared (static in C#),
Global or in a method. His own reference will set to nothing: as method
goes out of scope, as its object class goes out of scope "me" ("this" in C#)
or as soon as the program stops; or as a reference is set to another object.

But that is not everything, it will not be removed as long as another
something is pointing to the object, while that object itself is not an
object in the list of that.

An Example.

Drag a datagridview on your form, it will be set in the global part of your
form
Create a datatable with a row and add that to your datatable row collection.
Set that datatable as the dataset of your grid

Now the datatable will not be removed as long as you don't set the
dataSource of the datagridview to nothing or to another datatable.

This is an example as we have often seen here, where people thought it went
wrong (a bug) because they did not understand this.

Cor


>
That's not what I said.

Consider the objects A, B, and C, and -denoting a reference:

A -B -C

If A can be reached from the "running code", B and C cannot be finalized.

However, when removing the reference from A to B

A B -C
Oct 26 '08 #63
On Sat, 25 Oct 2008 03:47:55 +0200, "Herfried K. Wagner [MVP]" wrote:
>No, the reference stored in the local variable would have been removed
automaticall y. However, there were some pitfalls: Jumping out of a 'With'
block, for example, led to a reference which could not be removed but
prevented the object from being destroyed:
Good point, forgot to mention this.

Jumping out of a With block is generally dangerous as can be seen
here:

'***
Option Explicit

Private Type TestType
Value As Long
End Type

Sub main()
Dim tt() As TestType
Dim l As Long
ReDim tt(2)
l = 0
Do
With tt(l)
.Value = l
If .Value = 2 Then Exit Do 'jumps out of block
End With
l = l + 1
Loop
ReDim Preserve tt(1) '->fails with RTE 10, array is locked
End Sub
'***

However, you were talking about *some* pitfalls and jumping out of a
With block as an *example*. Which other situations that lead to
omitted dereferencing do you have in mind?

Wolfgang
Oct 27 '08 #64
"Wolfgang Enzinger" <we******@tempo raryforwarding. comschrieb:
However, you were talking about *some* pitfalls and jumping out of a
With block as an *example*. Which other situations that lead to
omitted dereferencing do you have in mind?
Well, I only had the particular scenario I showed in my post in my mind, but
I was thinking about different reasons for jumping out of a 'With' block.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Oct 28 '08 #65
I asked around and got this response:
Most "serious" apps that Microsoft ships (including the OS) has a mix of
both .NET managed code and COM/ASM code--some more than others. Some are
nearly all .NET while others are built to use COM and need to be implemented
with COM.

--
_______________ _______________ _______________ _______________ ______________
William R. Vaughn
President and Founder Beta V Corporation
Author, Mentor, Dad, Grandpa
Microsoft MVP
(425) 556-9205 (Pacific time)
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
_______________ _______________ _______________ _______________ _______________ _______________ __

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.atwrote in message
news:uv******** ******@TK2MSFTN GP05.phx.gbl...
"Wolfgang Enzinger" <we******@tempo raryforwarding. comschrieb:
>However, you were talking about *some* pitfalls and jumping out of a
With block as an *example*. Which other situations that lead to
omitted dereferencing do you have in mind?

Well, I only had the particular scenario I showed in my post in my mind,
but I was thinking about different reasons for jumping out of a 'With'
block.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Oct 30 '08 #66
Hi Bill,

I get the idea that you are answering a message from somebody in a newsgroup
which has removed some groups in the grouplist.

In the VB language group it shows up as an answer to Herfried, but it has in
my idea not anything to do with his message.

Cor

"William Vaughn (MVP)" <bi****@NoSpamB etav.comwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>I asked around and got this response:
Most "serious" apps that Microsoft ships (including the OS) has a mix of
both .NET managed code and COM/ASM code--some more than others. Some are
nearly all .NET while others are built to use COM and need to be
implemented with COM.

--
_______________ _______________ _______________ _______________ ______________
William R. Vaughn
President and Founder Beta V Corporation
Author, Mentor, Dad, Grandpa
Microsoft MVP
(425) 556-9205 (Pacific time)
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
_______________ _______________ _______________ _______________ _______________ _______________ __

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.atwrote in message
news:uv******** ******@TK2MSFTN GP05.phx.gbl...
>"Wolfgang Enzinger" <we******@tempo raryforwarding. comschrieb:
>>However, you were talking about *some* pitfalls and jumping out of a
With block as an *example*. Which other situations that lead to
omitted dereferencing do you have in mind?

Well, I only had the particular scenario I showed in my post in my mind,
but I was thinking about different reasons for jumping out of a 'With'
block.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Oct 30 '08 #67

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

Similar topics

67
1988
by: John | last post by:
Hi What are the advantages actually achieved of managed code? I am not talking of theory but in reality. Thanks Regards
0
9579
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
10578
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10321
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,...
1
7620
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
6853
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
5522
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
4300
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
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.