By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,494 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,494 IT Pros & Developers. It's quick & easy.

Async file read causes memory leak

P: n/a
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[i] = (scaling[channel, 2] *
(double)currentShort
+ scaling[channel, 1]) * scaling[channel, 3];
}

return scaledChunk;
}

Jun 7 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Jun 7, 10:56 am, kreut...@gmail.com wrote:
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

Jun 7 '07 #2

P: n/a
On 7 Jun, 11:39, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
On Jun 7, 10:56 am, kreut...@gmail.com wrote:
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
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?

Jun 7 '07 #3

P: n/a
On Jun 7, 3:54 pm, kreut...@gmail.com wrote:
On 7 Jun, 11:39, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
On Jun 7, 10:56 am, kreut...@gmail.com wrote:
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

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

Jun 7 '07 #4

P: n/a
On Jun 7, 11:54 am, kreut...@gmail.com wrote:
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

Jun 7 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.