Re: Memory Allocation in a Multi-Threaded Environment




"David Lowndes" <DavidL@xxxxxxxxxxxxxxx> wrote in message
news:dnkn44l5vhk751e51pb8t39nu7tmirq0do@xxxxxxxxxx
I quickly realised that multi-threading opens up a whole new slew of bugs
for the novice, but there is one area that is really confusing me.
I have a small class that uses an std::vector array. I create a new worker
process with afxbeginthread - this process will expand/contract the vector
(and do some other stuff) and then return. The problem is, I seem to get
heap corruption errors when I try to access the memory allocated in the
worker thread after that thread has finised. Is this to be expected or
should I be looking elswhere for the bug?

Jeff,

It's not expected, but without more details it's nigh on impossible to
know where your error lies. Try the same code in a single threaded
test and see if you suffer the same issue.


Thanks a lot for that, I guess the issue must be somewhere else then.

The particular example concerns reading and processing data from a URL file.
A worker thread is created to read the entire file into a buffer whilst the
main thread begins processing the data as it becomes available. The program
was working fine on my local host where the file was "downloaded" almost
before the processing had begun. When I try it on a remote file however the
heap corruption problem arises - I suspect because the processing is now
catching up with the download. I'm using a local data member to synchronise
reading and resizing of the buffer. In particular the buffer can only be
resized if it isn't being read and can only be read if it isn't being
resized - since a resize could move the buffer. I assume that the buffer can
be read and written to simultaneously.

If I do the download before (and in the same thread as) the processing then
I have no problem even with a remote Url.

Does anyone have a pointer to a simple program that does this type of
simultaneous buffer and process operation?

Thanks again
Jeff


.



Relevant Pages

  • Re: Assembly conversion to Pocket Pc format
    ... But in my case it is throwing exception before executing that statement what ... That is before reading buffer size it is throwing ... Stream respStream = resp.GetResponseStream; ... upload and download files in compact framework. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Memory Allocation in a Multi-Threaded Environment
    ... In general, I believe if you are sharing the same data between two threads, the design is ... A worker thread is created to read the entire file into a buffer whilst the ... catching up with the download. ... If you have to wait to resize until the processing is done, ...
    (microsoft.public.vc.mfc)
  • Library design for downloading an unknown amount of data?
    ... I'm writing a library to download data from a number of external devices. ... Devices which support random access to the internal memory. ... unsigned int address, ... To use this function, you need to pass a buffer that is large enough the store the downloaded data, and you get the actual size in the "result" parameter after the download. ...
    (comp.lang.c)
  • Re: Library design for downloading an unknown amount of data?
    ... download data from a number of external devices. ... A potential problem with this approach is that the buffer size will be ... Some devices send everything in a single packet, ... download everything, cache the data, return it chunk by chunk to the ...
    (comp.lang.c)
  • Re: Library design for downloading an unknown amount of data?
    ... download data from a number of external devices. ... unsigned int* size, ... exception that I'm returning an error code if the buffer is too small. ... Some devices send everything in a single packet, ...
    (comp.lang.c)

Loading