Board index » cppbuilder » Queue in a thread problem, content no more valid

Queue in a thread problem, content no more valid

2006-09-14 01:12:32 PM
Hello, I have a problem using a queue in a thread.
I have a thread working with a queue. This queue contains POINTERS to my
class TS2IRecord
(see my attachments under newsgroup borland.public.attachments from
class TS2IRecord
private: AnsiString A;
AnsiString B;
AnsiString C;
In the function Start (TMyThread.cpp) strange things happen on debugging:
I set a breakpoint to the line
TS2IRecord *Test0 = RecordWriteFIFO.front(); // In Function Start in
Then I execute the program. It stops at the breakpoint above.
Then I move the cursor to next line: RecordWriteFIFO.pop(); and press F4 (to
execute until cursor)
The line *Test0 = RecordWriteFIFO.front(); is executed and the cursor stops
at line RecordWriteFIFO.pop(); as expected.
Then *Test0 contains garbage. The content ABC, DEF, GHI is not available
anymore, it shows only garbage like uninitilaized memory.
But if I set the breakpoint directly to RecordWriteFIFO.pop(); and execute
MyApp then *Test0 contains "ABC", "DEF", "GHI" like requested.
So what is wrong there? I cannot understand!
I need this to free the occupied memory by TS2IRecord correctly, but if my
pointer just points to garbage, how to free?
The thing I want to achieve is:
1. Fill a read FIFO (queue) with pointers to TS2IRecord by creating new
instances of TS2IRecord and push their pointers on th read FIFO.
2. Do something with the TS2IRecord instances by accessing them over their
3. If thread is terminated pop all pointers and free the instances
What I am doing wrong?
Thanks for any help!

Re:Queue in a thread problem, content no more valid

I still need help, please can somone support me?
Thanks a lot!

Re:Queue in a thread problem, content no more valid

The problem is in Button2Click(). You are deleting 'MyThread' immediately
after the call to Terminate(). Terminate is a non-blocking method which
means most likely the delete will happen before the thread's Execute method
is finished. Either use MyThread->WaitFor() or MyThread->FreeOnTerminate
without the delete and your results will be better.
Sorry it took so long for you to get an answer. I figured Remy would
eventually look at the code he asked you to put up. But after I saw your
latest plea for help I figured I'd stick my nose in.
- Clayton


Re:Queue in a thread problem, content no more valid

Hello Clayton,
I was a few days off, so only now I could read your suggestions.
What you wrote sounds good, will try this next days.
Thanks so much for your support!