ArrayList and its Insert method

Z

Zeng

Hello Everyone,

Is there anything wrong with this block of code?

ArrayList arr = new ArrayList( 33 );

for( int i = 0; i < 33; ++i )
{
arr.Insert( i, new MyObject() );
}

It's so strange that when my web application have another thread allocating
and deallocating bit junks of memory at the same time, I occasionally got
ArgumentOutOfRangeException from the the insert method. And the states of
the variables at the exception are:
i = 31
arr.Count = 4 // this is strange, shouldn't it be 31?

If I use arr.Add method instead, it works better. Has anyone seen something
like this before or something I misunderstood about ArrayList::Insert()
method? Thanks!

-zeng
 
J

Jon Skeet [C# MVP]

Zeng said:
Is there anything wrong with this block of code?

ArrayList arr = new ArrayList( 33 );

for( int i = 0; i < 33; ++i )
{
arr.Insert( i, new MyObject() );
}

It's so strange that when my web application have another thread allocating
and deallocating bit junks of memory at the same time, I occasionally got
ArgumentOutOfRangeException from the the insert method. And the states of
the variables at the exception are:
i = 31
arr.Count = 4 // this is strange, shouldn't it be 31?

If I use arr.Add method instead, it works better. Has anyone seen something
like this before or something I misunderstood about ArrayList::Insert()
method? Thanks!

I think the key here is that you've got another thread involved. Is the
other thread doing anything with the ArrayList?

Note that the parameter you pass into the constructor is irrelevant in
terms of insertions - it only gives an initial buffer size.
 
Z

Zeng

No, the other thread doesn't do anything to the array list and the array
list is a local object doesn't get passed around at all.
 
Z

Zeng

I couldn't reproduce it in my local development environment. I only saw it
in the production server where many of our users use the site at the same
time. I have tried to force garbage collection between the Insert calls but
no luck in reproducing it although there isn't another thread involved.
Since I replaced it with Add() method, it's been working fine in production.
From what I understand there isn't a difference between Insert( i, obj) and
Add( obj ) if i = count of the array object. Could it be memory management
issue with .net, its garbage collector might collect something by mistake?
:)

By the way, I couldn't open the link you referred me to. Thanks!
-zeng
 
J

Jon Skeet [C# MVP]

Zeng said:
I couldn't reproduce it in my local development environment. I only saw it
in the production server where many of our users use the site at the same
time. I have tried to force garbage collection between the Insert calls but
no luck in reproducing it although there isn't another thread involved.
Since I replaced it with Add() method, it's been working fine in production.
From what I understand there isn't a difference between Insert( i, obj) and
Add( obj ) if i = count of the array object. Could it be memory management
issue with .net, its garbage collector might collect something by mistake?
:)

It shouldn't be. Are you positive that both i and the array list
variables are local variables?
By the way, I couldn't open the link you referred me to. Thanks!

That's odd - the link itself is okay. Try
http://www.yoda.arachsys.com/csharp/complete.html
 

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