Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A Static property returns a List with generated Simulation Data.

In this example I create IPAddress from strings to simulate IPs from a mobile device.
What are the risc of memory leaks?
C#
static readonly string[] IPv6Simu = { "fe80::3a6d:eeeb:8bff:4ef2", "fc01:abab:cdcd:efe0:49d2:473:579c:cfaa" };
   static readonly string[] IPv4Simu = { "172.22.1.100", "172.22.1.100" };
   static readonly string[] InterfaceName = { "mnet1", "mnet0" };

  public static List<IpData> StaticSimulateIPFactory
   { get
       {
           List<IpData> simu = new List<IpData>();
           try
           {
               IPAddress ip;
               foreach (var IPv6 in IPv6Simu)
               {
                   if (IPAddress.TryParse(IPv6, out ip))
                   {
                       simu.Add(new IpData() { InterfaceName = InterfaceName.FirstOrDefault(), IPAddress = ip });
                   }
               }
               foreach (var IPv4 in IPv4Simu)
               {
                   if (IPAddress.TryParse(IPv4, out ip))
                   {
                       simu.Add(new IpData() { InterfaceName = InterfaceName.FirstOrDefault(), IPAddress = ip });
                   }
               }
               return simu;
           }
           catch (Exception)
           {
               simu.Add(new IpData() { InterfaceName = "INV", IPAddress = new IPAddress(new byte[]{127,0,0,1} )});
               return simu;
           }
       }
   }


What I have tried:

The other approach is to make all non static. not a Problem, but I want to use this Simulation Value in a static method.
Posted
Updated 1-Aug-16 22:54pm
v2
Comments
lukeer 1-Aug-16 7:01am    
I don't see any risk of a memory leak in this code.
But the fact that you're asking makes me believe that you think of static to mean something that it actually doesn't.

The static keyword is not about memory management but about visibility (like private, public, etc).
Richard Deeming 1-Aug-16 9:41am    
Er, no.

The static keyword is nothing to do with visibility. It's perfectly possible to have public static or private static or internal static.

The static keyword is about whether a member belongs to the type itself, or belongs to an instance of the type.
lukeer 2-Aug-16 6:55am    
Ok, wrong word. Maybe accessibility?
You cannot access instance variables from within a static method (unless creating an instance and using that, but you get what I mean, right?) but you can access static variables from within instance methods.
Tomas Takac 1-Aug-16 10:41am    
Dump the static, make it all instance and then call it like this: new IPFactory.Simulate(). You should post the code where this is used.

1 solution

No memory leak - wyh do you think that? - but no need to recreate the same list again and again on every property access. Just make a static List variable and initialize once (if null), then return the list.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900