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

lock and object passing by ref

Hi,

I get the following warning:
"Possibly incorrect assignment to local 'oLockObject' which is the argument
to a using or lock statement. The Dispose call or unlocking will happen on
the original value of the local."

My code is:
using System;
using System.Collections.Generic;
using System.Text;

namespace RefLockExample
{
class Program
{

protected void MyMethod(ref Object oObject)
{
//something here
//oObject will be used, but no "new" object will be assigned to
oObject
}

static void Main(string[] args)
{
Program oProgram= new Program();
Object oLockObject;
oLockObject = new Object();
lock (oLockObject)
{
oProgram.MyMethod(ref oLockObject);

}

}
}
}

Why does this warning occur?
If I understand it right, it should not make much of a difference to leave
out the "ref".
What does actually get passed here?
I understood that object passing in .NET is passing the reference anyways
and not the value.
So, do I pass the reference on an object reference or do I pass a reference
on the object?

in the called method of course I will not assign anything "new" to the
object, but a problem with assigning anything new would be the same if I
would not have used "ref" also.
What can happen if I use this construct (example) instead of leaving out the
"ref"?

Regards,

Nov 28 '05 #1
6 5916
MSDNAndi wrote:
I get the following warning:
"Possibly incorrect assignment to local 'oLockObject' which is the argument
to a using or lock statement. The Dispose call or unlocking will happen on
the original value of the local."
Yup.

<snip>
Why does this warning occur?
Because the value of oLockObject may change within the lock.
If I understand it right, it should not make much of a difference to leave
out the "ref".
You don't understand it right, I'm afraid.
What does actually get passed here?
The variable is being passed by reference.
I understood that object passing in .NET is passing the reference anyways
and not the value.
The value of that variable *is* a reference. By default, parameter
passing is always done by value - but that value is often a reference.
This is *not* the same as passing the parameter *by* reference.
So, do I pass the reference on an object reference or do I pass a reference
on the object?


You're passing the variable oLockObject by reference.

I suggest you read http://www.pobox.com/~skeet/csharp/parameters.html

Jon

Nov 28 '05 #2
Jon,

thanks for your reply.

I think we mean similar things, but I just expressed myself badly.
I understand what value types, immutable types and "true" (mutable)
reference types are.
The problem of my posting was maybe that I did not point out clearly that I
wanted to talk about mutable reference types only where I will not change the
actual object assignment but only modify the object itself.
So with
"I understood that object passing in .NET is passing the reference anyways
and not the value.
....
So, do I pass the reference on an object reference or do I pass a reference
on the object?"
you replied
"
The value of that variable *is* a reference. By default, parameter
passing is always done by value - but that value is often a reference.
This is *not* the same as passing the parameter *by* reference.
....
You're passing the variable oLockObject by reference.
"
Thus in my (inaccurate) way of asking/expressing it you are saying (in this
specific case of a mutable reference type) that I pass an object reference
by reference.
(I know, "by reference" is different from passing the "reference" since the
object I am working on in the called component is not the passed reference,
but the "by reference" object).

Where I am unsure is what the compiler would do if I pass a mutable
reference type by reference... that maybe the compiler would make no
difference between
ref SomeMutableReferenceType
and
SomeMutableReferenceType.
It would not surprise me if the compiler would "optimize" ref
SomeMutableReferenceType to SomeMutableReferenceType.

In your excellent article you e.g. write about passing a value type by
reference and tell the difference between passing a struct IntHolder and a
class IntHolder, but I do not see an example for the difference of passing a
(mutable) reference type by reference.
Thanks anyways,
Nov 28 '05 #3
MSDNAndi wrote:

<snip>
Where I am unsure is what the compiler would do if I pass a mutable
reference type by reference... that maybe the compiler would make no
difference between
ref SomeMutableReferenceType
and
SomeMutableReferenceType.
It would not surprise me if the compiler would "optimize" ref
SomeMutableReferenceType to SomeMutableReferenceType.
It can't do that - the semantics are completely different.
In your excellent article you e.g. write about passing a value type by
reference and tell the difference between passing a struct IntHolder and a
class IntHolder, but I do not see an example for the difference of passing a
(mutable) reference type by reference.


I give an example where a StringBuilder parameter is passed by
reference. The point is that when a parameter is passed by reference,
the value of the variable in the actual parameter can change, which it
can't when the parameter is passed by value. So:

public void MakeMeNull (StringBuilder sb)
{
sb = null;
}

....

StringBuilder x = new StringBuilder();
MakeMeNull(x);

does nothing, but:

public void MakeMeNull2 (ref StringBuilder sb)
{
sb = null;
}

StringBuilder x = new StringBuilder();
MakeMeNull2(ref x);

leaves x with a value of null.

Jon

Nov 28 '05 #4
MSDNAndi wrote:
Hi,

I get the following warning:
"Possibly incorrect assignment to local 'oLockObject' which is the argument
to a using or lock statement. The Dispose call or unlocking will happen on
the original value of the local."

My code is:
using System;
using System.Collections.Generic;
using System.Text;

namespace RefLockExample
{
class Program
{

protected void MyMethod(ref Object oObject)
{
//something here
//oObject will be used, but no "new" object will be assigned to
oObject
}


Simple example:

protected void MyMethod(ref Object oObject)
{
oObject = new Object();
}

this means that you're passing back a new object, so the following code:

lock (oLockObject)
{
MyMethod(ref oLockObject);
}

could mean that it locks one object, changes it through the method call,
and thus tries to unlock the new object. This is what the warning is
about, that this isn't going to happen, that it's the original object
(which is now replaced with a new one) that is going to be unlocked.

Of course, as you say, you don't replace the object, but the compiler
doesn't know that.
--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 28 '05 #5
Hi Jon,

yes, but I was not talking about assigning a newly created object or a
"null" value to the object, but only about the case where I modify the
content of the object.

class IntHolder
{
public int intValue;
IntHolder()
{}
}
Difference between
protected void ModifyIntholderByRef(ref IntHolder myIntHolder)
{
myIntHolder.intValue=1;
}

protected void ModifyIntholder(IntHolder myIntHolder)
{
myIntHolder.intValue=2;
}

I am not assigning anything to myIntHolder itself in the called methods.

IntHolder localIntHolder =new IntHolder()
localIntHolder.intValue=0;

with the calls:
ModifyIntHolderByRef(ref localIntHolder);

or:
ModifyIntHolder(localIntHolder);
What would be the difference in this example (again: no assignments to
myIntHolder in the methods, and no "structs")?

Thanks for you patience,
Nov 28 '05 #6
MSDNAndi wrote:
yes, but I was not talking about assigning a newly created object or a
"null" value to the object, but only about the case where I modify the
content of the object.
In that case, you shouldn't pass the parameter by reference - and the
compiler doesn't know that that will be the case.
with the calls:
ModifyIntHolderByRef(ref localIntHolder);

or:
ModifyIntHolder(localIntHolder);
The compiler doesn't know that ModifyIntHolderByRef doesn't really
change the value of the parameter - it's been told to pass something by
reference, so it's going to pass it by reference. Don't forget - other
things could get involved, such as the debugger - to maintain the
correct semantics, if localIntHolder is changed in the debugger at
runtime, that change must get propagated to the call site.
What would be the difference in this example (again: no assignments to
myIntHolder in the methods, and no "structs")?


The difference is in the method signature, which tells the runtime what
to do with the parameters.

If you have no intention of changing the value of a parameter, you
shouldn't declare the parameter as "ref".

Jon

Nov 28 '05 #7

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

Similar topics

0
by: Bruce Pullen | last post by:
DB2 v7.2 (FP7 - DB2 v7.1.0.68) on AIX 5.2.0.0. We're seeing unexpected single row (then commit) insert locking behaviour. We're seeing Applications that already hold row-level W locks in...
7
by: Sunny | last post by:
Hi, I can not understend completely the lock statement. Actally what is locked: 1. the part of the code between {...} or 2. the object in lock() In the docs is written: for 1: The lock...
0
by: Nashat Wanly | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaskdr/html/askgui06032003.asp Don't Lock Type Objects! Why Lock(typeof(ClassName)) or SyncLock GetType(ClassName) Is Bad Rico...
14
by: Sharon | last post by:
Hi all. I have an ArrayList and sometimes while enumerating through, i get an exception because another thread has added to the ArrayList. To solve this problem, i lock the enumeration, passing...
2
by: Fernando Rodrguez | last post by:
Hi, I've been reading the multithreading tutorial at http://www.yoda.arachsys.com/csharp/threads/locking.shtml (thanks Jon :-) and there's one thing I don't understand. Why do have to pass a...
8
by: nytimescnn | last post by:
I've read some discuession about lock() for thread-safe. I am wondering what will be the differce between below two code segment? Code 1: class A { private static Object padlock = new...
2
by: shenanwei | last post by:
DB2 V8.2 on AIX, type II index is created. I see this from deadlock event monitor. 5) Deadlocked Connection ... Participant no.: 2 Lock wait start time: 09/18/2006 23:04:09.911774 .........
20
by: Kurt | last post by:
Below is a class that can accessed from multiple threads and I want the class to be thread safe. I have a private timer member whose interval can be changed by different threads. Which is the correct...
8
by: not_a_commie | last post by:
I'd like to be able to overload the lock command so that I could log an entry on lock and unlock. Any ideas on how to do this? I think it would be powerful if you could inherit from a few...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.