statics & memory

G

Guest

We have a C# app that runs on the PocketPC. We are having some odd behavior
reported from our users that I have been unable to recreate. It made me
start to look at memory issues. One of the appl errors that they get can
only come up when a static member variable inside a class is set to NULL.
The setting of that value is pretty controlled and I don't believe that the
value is changing to NULL through normal logic flow.

the class itself is not defined as static but all the access methods and
member variables are. Is there any situation where garbage collecting could
kick in and reset the memory inside the object. I don't actually ever
declare an instance of the class itself. Below is an abbreviated version of
the class definition:

namespace MobileTest
{
public class gDataMgr
{
public gDataMgr()
{
}

public static Staff CurrentStaff
{
get{ return m_CurrentStaff; }
}

public static long FacilityID
{
get{ return m_CMSFacilityID; }
set{ m_CMSFacilityID = value; }
}

public static ArrayList ActiveTimeList
{
get
{
if (m_ActiveTimeList == null)
{
m_ActiveTimeList = new ArrayList();
}
return m_ActiveTimeList;
}
set{ m_ActiveTimeList = value; }
}

public static bool Login(Staff staff)
{
m_CurrentStaff = staff;
}

private static Staff m_CurrentStaff;
private static long m_CMSFacilityID = 1;
private static ArrayList m_ActiveTimeList;
}
}

The m_CurrentStaff gets assigned by calling the Login method in this class.
The Staff does not get set to NULL until the user logs out.

This is the code that prints the app error message:

// inside some app logic
Staff currentStaff = gDataMgr.CurrentStaff;
if (currentStaff != null)
{
sDiscipline = currentStaff.StaffType;
}
else
{
MessageBox.Show("LOGIC ERROR: Unable to retrieve the current staff .");
return null;
}


Thanks for any assistance with this.

Regards,

-Rob C
 
N

Nicholas Paldino [.NET/C# MVP]

Rob,

I think the design here is a little off. For example, you can call the
static method Login multiple times, which will then set the user to the
user, or to null.

What I think you should do is encapsulate the logic in an instance,
allowing the user to log in once (through a method/constructor on the
instance), and then have a singleton which you access the properties of the
person (staff, timelist, etc, etc).

To me, right now, it appears that you are calling Login more than once,
and it is setting your variable to null (since you are passing null,
presumably).
 
G

Guest

The only time in the program where the variable is getting set to NULL is
when the user logs out. The error is occuring while they are still logged in.

Is there a problem with the fact that I don't explicitly create an instance
of the gDataMgr even though the class is not defined as a static. I know
this was OK with VC++.

Also, if the device was running out of memory, would the garbage collecting
ever free up the memory allocated for the class?

Regards,

Rob C


Nicholas Paldino said:
Rob,

I think the design here is a little off. For example, you can call the
static method Login multiple times, which will then set the user to the
user, or to null.

What I think you should do is encapsulate the logic in an instance,
allowing the user to log in once (through a method/constructor on the
instance), and then have a singleton which you access the properties of the
person (staff, timelist, etc, etc).

To me, right now, it appears that you are calling Login more than once,
and it is setting your variable to null (since you are passing null,
presumably).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rob C said:
We have a C# app that runs on the PocketPC. We are having some odd
behavior
reported from our users that I have been unable to recreate. It made me
start to look at memory issues. One of the appl errors that they get can
only come up when a static member variable inside a class is set to NULL.
The setting of that value is pretty controlled and I don't believe that
the
value is changing to NULL through normal logic flow.

the class itself is not defined as static but all the access methods and
member variables are. Is there any situation where garbage collecting
could
kick in and reset the memory inside the object. I don't actually ever
declare an instance of the class itself. Below is an abbreviated version
of
the class definition:

namespace MobileTest
{
public class gDataMgr
{
public gDataMgr()
{
}

public static Staff CurrentStaff
{
get{ return m_CurrentStaff; }
}

public static long FacilityID
{
get{ return m_CMSFacilityID; }
set{ m_CMSFacilityID = value; }
}

public static ArrayList ActiveTimeList
{
get
{
if (m_ActiveTimeList == null)
{
m_ActiveTimeList = new ArrayList();
}
return m_ActiveTimeList;
}
set{ m_ActiveTimeList = value; }
}

public static bool Login(Staff staff)
{
m_CurrentStaff = staff;
}

private static Staff m_CurrentStaff;
private static long m_CMSFacilityID = 1;
private static ArrayList m_ActiveTimeList;
}
}

The m_CurrentStaff gets assigned by calling the Login method in this
class.
The Staff does not get set to NULL until the user logs out.

This is the code that prints the app error message:

// inside some app logic
Staff currentStaff = gDataMgr.CurrentStaff;
if (currentStaff != null)
{
sDiscipline = currentStaff.StaffType;
}
else
{
MessageBox.Show("LOGIC ERROR: Unable to retrieve the current staff .");
return null;
}


Thanks for any assistance with this.

Regards,

-Rob C
 
N

Nicholas Paldino [.NET/C# MVP]

Rob,

No, the GC mechanism will not touch a static variable. There is always
a strong reference to it through the type. You have to be setting it
explicitly to null somewhere.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rob C said:
The only time in the program where the variable is getting set to NULL is
when the user logs out. The error is occuring while they are still logged
in.

Is there a problem with the fact that I don't explicitly create an
instance
of the gDataMgr even though the class is not defined as a static. I know
this was OK with VC++.

Also, if the device was running out of memory, would the garbage
collecting
ever free up the memory allocated for the class?

Regards,

Rob C


Nicholas Paldino said:
Rob,

I think the design here is a little off. For example, you can call
the
static method Login multiple times, which will then set the user to the
user, or to null.

What I think you should do is encapsulate the logic in an instance,
allowing the user to log in once (through a method/constructor on the
instance), and then have a singleton which you access the properties of
the
person (staff, timelist, etc, etc).

To me, right now, it appears that you are calling Login more than
once,
and it is setting your variable to null (since you are passing null,
presumably).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rob C said:
We have a C# app that runs on the PocketPC. We are having some odd
behavior
reported from our users that I have been unable to recreate. It made
me
start to look at memory issues. One of the appl errors that they get
can
only come up when a static member variable inside a class is set to
NULL.
The setting of that value is pretty controlled and I don't believe that
the
value is changing to NULL through normal logic flow.

the class itself is not defined as static but all the access methods
and
member variables are. Is there any situation where garbage collecting
could
kick in and reset the memory inside the object. I don't actually ever
declare an instance of the class itself. Below is an abbreviated
version
of
the class definition:

namespace MobileTest
{
public class gDataMgr
{
public gDataMgr()
{
}

public static Staff CurrentStaff
{
get{ return m_CurrentStaff; }
}

public static long FacilityID
{
get{ return m_CMSFacilityID; }
set{ m_CMSFacilityID = value; }
}

public static ArrayList ActiveTimeList
{
get
{
if (m_ActiveTimeList == null)
{
m_ActiveTimeList = new ArrayList();
}
return m_ActiveTimeList;
}
set{ m_ActiveTimeList = value; }
}

public static bool Login(Staff staff)
{
m_CurrentStaff = staff;
}

private static Staff m_CurrentStaff;
private static long m_CMSFacilityID = 1;
private static ArrayList m_ActiveTimeList;
}
}

The m_CurrentStaff gets assigned by calling the Login method in this
class.
The Staff does not get set to NULL until the user logs out.

This is the code that prints the app error message:

// inside some app logic
Staff currentStaff = gDataMgr.CurrentStaff;
if (currentStaff != null)
{
sDiscipline = currentStaff.StaffType;
}
else
{
MessageBox.Show("LOGIC ERROR: Unable to retrieve the current staff
.");
return null;
}


Thanks for any assistance with this.

Regards,

-Rob C
 
C

Cor Ligthert [MVP]

Nick,

Can you make a sample from that, I am always interested how something that
is static becomes nonstatic, I had always the idea that Static did mean to
be static in a program (although that is not really true more these days
because the relocation of the program inside the OS, but let us forget
that).

Therefore do you have an example?

Cor

Nicholas Paldino said:
Rob,

No, the GC mechanism will not touch a static variable. There is always
a strong reference to it through the type. You have to be setting it
explicitly to null somewhere.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rob C said:
The only time in the program where the variable is getting set to NULL is
when the user logs out. The error is occuring while they are still
logged in.

Is there a problem with the fact that I don't explicitly create an
instance
of the gDataMgr even though the class is not defined as a static. I know
this was OK with VC++.

Also, if the device was running out of memory, would the garbage
collecting
ever free up the memory allocated for the class?

Regards,

Rob C


Nicholas Paldino said:
Rob,

I think the design here is a little off. For example, you can call
the
static method Login multiple times, which will then set the user to the
user, or to null.

What I think you should do is encapsulate the logic in an instance,
allowing the user to log in once (through a method/constructor on the
instance), and then have a singleton which you access the properties of
the
person (staff, timelist, etc, etc).

To me, right now, it appears that you are calling Login more than
once,
and it is setting your variable to null (since you are passing null,
presumably).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

We have a C# app that runs on the PocketPC. We are having some odd
behavior
reported from our users that I have been unable to recreate. It made
me
start to look at memory issues. One of the appl errors that they get
can
only come up when a static member variable inside a class is set to
NULL.
The setting of that value is pretty controlled and I don't believe
that
the
value is changing to NULL through normal logic flow.

the class itself is not defined as static but all the access methods
and
member variables are. Is there any situation where garbage collecting
could
kick in and reset the memory inside the object. I don't actually ever
declare an instance of the class itself. Below is an abbreviated
version
of
the class definition:

namespace MobileTest
{
public class gDataMgr
{
public gDataMgr()
{
}

public static Staff CurrentStaff
{
get{ return m_CurrentStaff; }
}

public static long FacilityID
{
get{ return m_CMSFacilityID; }
set{ m_CMSFacilityID = value; }
}

public static ArrayList ActiveTimeList
{
get
{
if (m_ActiveTimeList == null)
{
m_ActiveTimeList = new ArrayList();
}
return m_ActiveTimeList;
}
set{ m_ActiveTimeList = value; }
}

public static bool Login(Staff staff)
{
m_CurrentStaff = staff;
}

private static Staff m_CurrentStaff;
private static long m_CMSFacilityID = 1;
private static ArrayList m_ActiveTimeList;
}
}

The m_CurrentStaff gets assigned by calling the Login method in this
class.
The Staff does not get set to NULL until the user logs out.

This is the code that prints the app error message:

// inside some app logic
Staff currentStaff = gDataMgr.CurrentStaff;
if (currentStaff != null)
{
sDiscipline = currentStaff.StaffType;
}
else
{
MessageBox.Show("LOGIC ERROR: Unable to retrieve the current staff
.");
return null;
}


Thanks for any assistance with this.

Regards,

-Rob C
 
N

Nicholas Paldino [.NET/C# MVP]

There is no implication from my previous email that a static variable
becomes non static.

Static variables, once the type is instantiated, live for the life of
the app domain that they are in.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Cor Ligthert said:
Nick,

Can you make a sample from that, I am always interested how something that
is static becomes nonstatic, I had always the idea that Static did mean to
be static in a program (although that is not really true more these days
because the relocation of the program inside the OS, but let us forget
that).

Therefore do you have an example?

Cor

Nicholas Paldino said:
Rob,

No, the GC mechanism will not touch a static variable. There is
always a strong reference to it through the type. You have to be setting
it explicitly to null somewhere.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rob C said:
The only time in the program where the variable is getting set to NULL
is
when the user logs out. The error is occuring while they are still
logged in.

Is there a problem with the fact that I don't explicitly create an
instance
of the gDataMgr even though the class is not defined as a static. I
know
this was OK with VC++.

Also, if the device was running out of memory, would the garbage
collecting
ever free up the memory allocated for the class?

Regards,

Rob C


:

Rob,

I think the design here is a little off. For example, you can call
the
static method Login multiple times, which will then set the user to the
user, or to null.

What I think you should do is encapsulate the logic in an instance,
allowing the user to log in once (through a method/constructor on the
instance), and then have a singleton which you access the properties of
the
person (staff, timelist, etc, etc).

To me, right now, it appears that you are calling Login more than
once,
and it is setting your variable to null (since you are passing null,
presumably).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

We have a C# app that runs on the PocketPC. We are having some odd
behavior
reported from our users that I have been unable to recreate. It made
me
start to look at memory issues. One of the appl errors that they get
can
only come up when a static member variable inside a class is set to
NULL.
The setting of that value is pretty controlled and I don't believe
that
the
value is changing to NULL through normal logic flow.

the class itself is not defined as static but all the access methods
and
member variables are. Is there any situation where garbage
collecting
could
kick in and reset the memory inside the object. I don't actually
ever
declare an instance of the class itself. Below is an abbreviated
version
of
the class definition:

namespace MobileTest
{
public class gDataMgr
{
public gDataMgr()
{
}

public static Staff CurrentStaff
{
get{ return m_CurrentStaff; }
}

public static long FacilityID
{
get{ return m_CMSFacilityID; }
set{ m_CMSFacilityID = value; }
}

public static ArrayList ActiveTimeList
{
get
{
if (m_ActiveTimeList == null)
{
m_ActiveTimeList = new ArrayList();
}
return m_ActiveTimeList;
}
set{ m_ActiveTimeList = value; }
}

public static bool Login(Staff staff)
{
m_CurrentStaff = staff;
}

private static Staff m_CurrentStaff;
private static long m_CMSFacilityID = 1;
private static ArrayList m_ActiveTimeList;
}
}

The m_CurrentStaff gets assigned by calling the Login method in this
class.
The Staff does not get set to NULL until the user logs out.

This is the code that prints the app error message:

// inside some app logic
Staff currentStaff = gDataMgr.CurrentStaff;
if (currentStaff != null)
{
sDiscipline = currentStaff.StaffType;
}
else
{
MessageBox.Show("LOGIC ERROR: Unable to retrieve the current staff
.");
return null;
}


Thanks for any assistance with this.

Regards,

-Rob C
 
S

SP

Rob C said:
We have a C# app that runs on the PocketPC. We are having some odd
behavior
reported from our users that I have been unable to recreate. It made me
start to look at memory issues. One of the appl errors that they get can
only come up when a static member variable inside a class is set to NULL.
The setting of that value is pretty controlled and I don't believe that
the
value is changing to NULL through normal logic flow.

the class itself is not defined as static but all the access methods and
member variables are. Is there any situation where garbage collecting
could
kick in and reset the memory inside the object. I don't actually ever
declare an instance of the class itself. Below is an abbreviated version
of
the class definition:

public static bool Login(Staff staff)
{
m_CurrentStaff = staff;

Add a null check here as this would seem to be the only place that
m_CurrentStaff could be getting passed a null. Show us the code that calls
this method?

SP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top