Async file read causes memory leak

  • Thread starter Thread starter kreuters
  • Start date Start date
K

kreuters

hey,

can anyone explain to me why this method that is called numerous times
to read chunks of a file works fine with a normal synchronous read,
but causes a large memory leak when BeginRead() is called instead?

private double[] ReadChunk(FileStream fileReader, int
readLength, int channel)
{
short currentShort;
int i;
byte[] inBuffer = new byte[2 * readLength];
double[] scaledChunk = new double[readLength];

// causes massive memory leak, why?
IAsyncResult ar = fileReader.BeginRead(inBuffer, 0,
inBuffer.Length, null, null);
ar.AsyncWaitHandle.WaitOne();

// works fine if i just call this
//fileReader.Read(inBuffer, 0, inBuffer.Length);

for (i = 0; i < scaledChunk.Length; i++)
{
currentShort = (short)((inBuffer[2 * i] << 8) +
inBuffer[2 * i + 1]);
scaledChunk = (scaling[channel, 2] *
(double)currentShort
+ scaling[channel, 1]) * scaling[channel, 3];
}

return scaledChunk;
}
 
can anyone explain to me why this method that is called numerous times
to read chunks of a file works fine with a normal synchronous read,
but causes a large memory leak when BeginRead() is called instead?

Are you ever calling EndRead? If not, that's probably the problem.

(I assume that in your real code there's a point to using asynchronous
IO. If you're just going to wait for it to complete, you might as well
do it synchronously to start with.)

Jon
 
Are you ever calling EndRead? If not, that's probably the problem.

(I assume that in your real code there's a point to using asynchronous
IO. If you're just going to wait for it to complete, you might as well
do it synchronously to start with.)

Jon

hey jon

yes, there is a point (i think) in reading asynchronously, in that
this method is going to be called many times by separate threads
running at the same time. am i correct in thinking that using async
reads will improve performance?

where would i call EndRead(), in the BeginRead callback delegate?
 
hey jon

yes, there is a point (i think) in reading asynchronously, in that
this method is going to be called many times by separate threads
running at the same time. am i correct in thinking that using async
reads will improve performance?

where would i call EndRead(), in the BeginRead callback delegate?

Call EndRead in a call back method.

public static void ReadFileCallback(IAsyncResult asyncResult)
{

//Get the state using asyncResult.AsyncState and get the stream from
state.
//Then call stream.EndRead(asyncResult);. This woud return the size of
data read.
}

Refere Async IO programming in MSDN:http://msdn2.microsoft.com/en-us/
library/aa719596(VS.71).aspx
 
yes, there is a point (i think) in reading asynchronously, in that
this method is going to be called many times by separate threads
running at the same time. am i correct in thinking that using async
reads will improve performance?

No - because you're already multi-threaded by the fact that the method
is going to be called many times by many separate threads! By blocking
until the read has finished, you've effectively got much more
complicated code but synchronous IO (per thread).
where would i call EndRead(), in the BeginRead callback delegate?

That's a fairly common place to do it, yes.

Jon
 
Back
Top