c# pass object by reference, wrapped TCP socket

I have an application that has a primary and secondary data source that it connects to. I create and update an object when the primary server  crashes so that all requests will be done on the secondary until it comes back up.
public class RequestStream
    {
        private TcpClient socket;
        private NetworkStream stream;
        private String address;
        private String port;

        public RequestStream(String aAddress, String aPort)
        {
            address = aAddress;
            port = aPort;
            try
            {
                socket = new TcpClient();
                socket.Connect(address, int.Parse(port));
                stream = socket.GetStream();
            }
            catch (Exception ex)
            {
               //
            }
        }

Open in new window

I have a separate thread that "heartbeats" the servers to check their status. When I need to change servers I trigger the following code situated on the DataStore object.
public void resetAllThreads()
        {
            try
            {
                lock (_locker)
                {
                    requestStream = new RequestStream(jeqiReference.GetAddress(), jeqiReference.GetPort());
                }
            }
            catch (Exception ex)
            {
               //
            }
        }

Open in new window

A breakpoint shows this is working correctly.

I pass a copy of the RequestStream to the constructor of an object called Raceday
requestStream = new RequestStream(jeqiReference.GetAddress(), jeqiReference.GetPort());

raceDay = new RaceDay(jeqiReference, ref requestStream);

Open in new window


The trouble is that the DataStore object correctly uses the new server but the Raceday object does not. On the Raceday object the adress stays the same but the stream is null. If I restart the Server and the object is updated again, the stream is now initiated again and works as before.

The Request stream object is passed through the raceday object to another set of objects, each of which again passes this through in other constructors.

How can I ensure the RequestStream in all the objects is the same object so this will work as intended?
LVL 1
jetbetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Miguel OzSoftware EngineerCommented:
Do not use parameter by reference. In your class ensure that Request stream object is passed by value:
public RaceDay (Whatever jeqiReference, RequestStream requestStream)
{//your code goes here}

Open in new window


When you are passing the object by value,  the value is a reference for reference types - a way of getting to the object (a pointer to memory location). The RaceDay class will not be able to change the stream object but only its public properties or whatever state is changed by public methods.
0
jetbetAuthor Commented:
I originally did not use the ref keyword and experienced the same issue.

I have removed it again but the problem remains.
0
sarabandeCommented:
On the Raceday object the adress stays the same but the stream is null.
since you created a new RequestStream and a new Raceday object, the old RequestStream and old Raceday object are no longer valid and are subject of the garbage collector (gc). that means the Raceday objetc you were looking for is no longer the one which should be alive but needs to be replaced by the new one.

the gc might be able to do this granted that there are no references to the old objects which prevent the old objects to be released and granted that you provided destructors which properly release and close all their open resources.

with other words: the statement
raceDay = new RaceDay(jeqiReference, ref requestStream);

Open in new window

should release and clear old raceDay object.

Sara
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

jetbetAuthor Commented:
Sorry I did not explain well enough.

The DataStore contains a Raceday and these are not destroyed until the Application closes.

The issue is with the RequestStream object that is used by The DataCenter, Raceday, Meeting, etc. type objects.

This is updated as necessary when there are issues with one of the servers. The problem is that there should be just one and each object needs a reference to it.

Each object that contains a reference can use it to update its own state via new information contained in an XML request/response via TCP
0
sarabandeCommented:
The problem is that there should be just one and each object needs a reference to it.
with
requestStream = new RequestStream(jeqiReference.GetAddress(), jeqiReference.GetPort());

Open in new window

you create a new object and the old one is not deleted if there are still references to it. same applies for the Raceday object. if you want to have  singletons for those objects, you have to create them only once (with new) and then update the existing objects rather than to creating new ones. for example you could create temporarily new objects and 'assign' the new objects to the singletons.

Sara
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jetbetAuthor Commented:
That was it.
I had assumed that the link to the piece of memory where the RequestStream was stored would remain valid for the references.
Thanks for the help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.