ArrayList

P

Peter Schmitz

Hi everyone,

I've got the following problem: In my application I make use of two
ArrayList objects like this:

Public Sub Main()
LoadData()
End Sub

Public Function LoadData()
Dim list1 as new ArrayList()
[...Fill the arraylist...]
AnalyzeData(list1)
End Function

Public Function AnalyzeData(list1 as ArrayList)
Dim list2 as new ArrayList
list2 = list1
[...do stuff...]
list2.Clear()
End Function

Now, this code creates a major memory leak....calling GC.Collect() etc.
won't help. Can anyone tell me what happens, when I call list2 = list1 - is
the whole list copied, or just a pointer??
I know this is a stupid program structure, but for some reasons I have to
stick to it...:/

Greetings,

Peter
 
P

PvdG42

Peter Schmitz said:
Hi everyone,

I've got the following problem: In my application I make use of two
ArrayList objects like this:

Public Sub Main()
LoadData()
End Sub

Public Function LoadData()
Dim list1 as new ArrayList()
[...Fill the arraylist...]
AnalyzeData(list1)
End Function

Public Function AnalyzeData(list1 as ArrayList)
Dim list2 as new ArrayList
list2 = list1
[...do stuff...]
list2.Clear()
End Function

Now, this code creates a major memory leak....calling GC.Collect() etc.
won't help. Can anyone tell me what happens, when I call list2 = list1 -
is
the whole list copied, or just a pointer??
I know this is a stupid program structure, but for some reasons I have to
stick to it...:/

Greetings,

Peter

Here is a complete sample that deonstrates what happens when a statement
like "list2 = list1
is executed.

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesArrayList

Public Shared Sub Main()

' Creates and initializes a new ArrayList.
Dim myAL As New ArrayList()
myAL.Add("Hello")
myAL.Add("World")
myAL.Add("!")

' Displays the properties and values of the ArrayList.
Console.WriteLine("myAL")
Console.WriteLine(" Count: {0}", myAL.Count)
Console.WriteLine(" Capacity: {0}", myAL.Capacity)
Console.Write(" Values:")
PrintValues(myAL)

Dim al2 As New ArrayList
al2 = myAL

' Displays the properties and values of the ArrayList.
Console.WriteLine("al2")
Console.WriteLine(" Count: {0}", al2.Count)
Console.WriteLine(" Capacity: {0}", al2.Capacity)
Console.Write(" Values:")
PrintValues(al2)
End Sub

Public Shared Sub PrintValues(ByVal myList As IEnumerable)
Dim obj As [Object]
For Each obj In myList
Console.Write(" {0}", obj)
Next obj
Console.WriteLine()
End Sub 'PrintValues

End Class
 

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