memory leak or not?

  • Thread starter Thread starter Guest
  • Start date Start date
Note, however, that *again* there is nothing magical about strings
here. If you had written:

SomeOtherType x = MyJonObject.CreateSomeOtherType();

But I did not, althouth you are able in the next reply to tell that I did,
because you wrote that.

However, by this you shows exactly in my opinion that I am right.

Cor
 
Jon,
You are writing here in a style which is *frequently* hard to
understand - and not just by me.
Said by probably the record holder of long newsgroups threads.

The normal (question) threads where I am active in are mostly not longer
than 3.
One for the question,
One for my answer
One for the thanks I get for the answer.

I am in this not speaking about this newsgroup because here I am not so
active as in 3 other major dotNet newsgroups.

Cor
 
Cor Ligthert said:
Jon,

Said by probably the record holder of long newsgroups threads.

The normal (question) threads where I am active in are mostly not
longer than 3.
One for the question,
One for my answer
One for the thanks I get for the answer.

I am in this not speaking about this newsgroup because here I am not
so active as in 3 other major dotNet newsgroups.

Cor

The reason for these lengthy threads involving you and Jon is that YOU
don't ever admit that YOU made a mistake. Or you don't grasp that you
HAVE made a mistake. Instead, you continue to insist that we are
twisting your words.

Here is your original post
<post>
Jon,

I am glad I can help you with this.
The string is immutable what you are showing is not a reference.
You can have a look at what immutable means at these pages.
http://www.yoda.arachsys.com/csharp/parameters.html

:-)

Cor
</post>

OK....strings are immutable....but what has that got to do with
anything? And what was returned was definitely a reference....so I am
not sure what you talking about???? The link to Jon's Page (cute) did
talk about immutable objects, but I still don't see the relevance to the
discussion. I suggest that you re-read the page yourself, because I
suspect that you lack of understanding of "immutable" as a large part of
the problem.

The only thing that I can think of to explain your statement is this
quote from Jon's page
<quote from Jon's page>
Note that many types (such as string) appear in some ways to be value
types, but in fact are reference types. These are known as immutable
types. This means that once an instance has been constructed, it can't
be changed. This allows a reference type to act similarly to a value
type in some ways - in particular, if you hold a reference to an
immutable object, you can feel comfortable in returning it from a method
or passing it to another method, safe in the knowledge that it won't be
changed behind your back. This is why, for instance, the string.Replace
doesn't change the string it is called on, but returns a new instance
with the new string data in - if the original string were changed, any
other variables holding a reference to the string would see the change,
which is very rarely what is desired.
</quote from Jon's page>

John talks about immutable type acting like value types instead of
reference types. This does not prevent immutable objects from obeying
the rules of garbage collection. It simply means that you know that the
object will not change.

None of this has anything to do with references or garbage collection
(the point of the original post).

Therefore, it is clear to me that what you wrote is not what you meant
or else you don't really understand references and immutability. Perhaps
we can clear this up for you.

Bill
 
| Jon,
|
| > You are writing here in a style which is *frequently* hard to
| > understand - and not just by me.
| >
| Said by probably the record holder of long newsgroups threads.
|
| The normal (question) threads where I am active in are mostly not longer
| than 3.
| One for the question,
| One for my answer
| One for the thanks I get for the answer.
|
| I am in this not speaking about this newsgroup because here I am not so
| active as in 3 other major dotNet newsgroups.
|
| Cor
|
|

Cor,

Everyone knows by now that Jon is right when he said "an instance of a type
CAN get GC'd BEFORE the reference, pointing to this instance, leaves it's
lexicaly-enclosing method scope".
So please, let this thread die if you can agree with this, else start a new
thread in which you explain why you think this is not the case.

Willy.
 
Bill,

Ok than, maybe I wrote it wrong and did Jon not understand that.
I am glad I can help you with this.
The string is immutable what you are showing is not a reference.
You can have a look at what immutable means at these pages.
http://www.yoda.arachsys.com/csharp/parameters.html
A string is immutable, what you are showing is not a reference *to an
existing object*.

I hope that this what I thought what was obvious makes it more clear.

Jon does not know that I think.

Cor
 
Willy,
Everyone knows by now that Jon is right when he said "an instance of a
type
CAN get GC'd BEFORE the reference, pointing to this instance, leaves it's
lexicaly-enclosing method scope".

But nobody including me has denied that, as well not Marina, who he was
correcting with his message.

Cor
 
Cor Ligthert said:
Ok than, maybe I wrote it wrong and did Jon not understand that.

A string is immutable, what you are showing is not a reference *to an
existing object*.

I hope that this what I thought what was obvious makes it more clear.

No, it's not obvious that's what you meant. "... what you are showing
is not a reference" and "... what you are showing is not a reference to
an existing object" are completely different statements, just like
"there are 29 days in February" and "there are 29 days in February in a
leap year" are completely different statements.

The funny thing is that when we *do* extrapolate what you actually
write into what we think you meant to write, you accuse us of putting
words into your mouth. Now you're trying to say we *should* try to
guess what you actually meant to write instead of believing that you
mean what you actually *do* write.


However, let's look at your revised statement:

"A string is immutable, what you are showing is not a reference to an
existing object."

Do you mean that the string must be newly created in the GrabLongString
method? If so, that's entirely untrue. GrabLongString() could be
implemented by:

return new string ('y', 1000); // Reference to newly created object

or

return someInstanceVariable; // Reference to "old" object


Furthermore, the "age" of an object has little to do with whether or
not it's eligible for garbage collection. Here are four different
implementations of GrabLongString:

1) New object, but keep object reference in a member variable: it won't
be eligible for garbage collection:

string value;
string GrabLongString()
{
value = new string('x', 10000);
return value;
}

2) New object, no other references to it: it will be eligible for
garbage collection after the call to Console.WriteLine in the calling
method:

string GrabLongString()
{
return new string ('x', 100000);
}

3) Existing object, we still have a reference to it after the method
returns: it won't be eligible for garbage collection:

string value = new string ('x', 10000);
string GrabLongString()
{
return value;
}

4) Existing object, but we change the value of the variable which was
holding the previous reference. Assuming nothing else references the
same string, it will be eligible for garbage colleciton after the call
to Console.WriteLine in the calling method:

string value = new string ('x', 10000);
string GrabLongString()
{
string ret = value;
value = null;
return ret;
}


I hope the above makes it clear that the value of x in the calling
method certainly *can* be a reference to an existing object, and that
it's irrelevant to garbage collection.

Furthermore, there's nothing in the above which is unique to strings,
or even to immutable types. *All* reference types obey the same rules.
Jon does not know that I think.

Know what, exactly? I still think that you're the one with some
incorrect ideas about strings here - however, you haven't articulated
those ideas clearly enough to prove them definitely wrong.

Perhaps you could try again to say in what way you think strings differ
from other reference types when it comes to garbage collection? If
you'd talked about string *literals* we'd be onto something, but you
haven't mentioned them so far, and most strings aren't created from
literals.

I asked you to specify clearly how you thought I was wrong a few posts
ago, but you chose to ignore technical arguments and concentrate on
personal ones...
 
Hi,
I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
Microsoft Online Community Support
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Back
Top