Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multi threading - variables going strange 1

Status
Not open for further replies.

RichardF

Programmer
Oct 9, 2000
239
GB
Hi,

my current project is a train simulator. the application links into a hardware interface which allows the software to receive messages when inputs are activated (for example the brake control).

I think I am getting problems with multithreaded programming, let me explain :

my app uses 3 threads :
main thread - this deals with network messages, window messages and sound
positional updates - this deals with moving the camera through the world
control input - this deals with hardware inputs and the reactions it causes (eg to slow the train)

Task:
The user should press and hold the engine start button for 20 seconds.
If he lets go before 20 seconds the engine will not start.

Problem:
When the button is pressed, i start a timer at 50ms intervals.
The timerproc counts each interval and tests if the button was released and plays the relevant sound.
The problem is - some of my data is going funny. I have an array of sound object pointers (pre-initialised) and index 8 will always goto 0x00000064 on the line :
iTimerID[EngineStartTimer] = SetTimer(SysWin->hWnd,EngineStartTimer,INTERVAL,SystemTimerProc);
where:
iTimerID is a global array of int
SysWin is a window object (instanced in the same thread, uses wndproc in the same thread)
EngineStartTimer is a enumerated type (value 100)
INTERVAL = 50
SystemTimerProc is the TimerProc.

Any help greatly appreciated.

thanks,
Rich.
 
So, what is going wrong?

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
> iTimerID is a global array of int
Which if it's iTimerID[8], means that "index 8 will always goto 0x00000064 on the line" will be off the end of the array.

> EngineStartTimer is a enumerated type (value 100)
Which is 0x00000064 - coincidence?



--
 
Hi,

thanks for your reply

Ion
cMusicSegment* Segment[MUSIC_BUFFERS];

The call to SetTimer (this is inside the control thread) changes the value of the Segment[8] (ie its address) from 0x01158478 to 0x00000064. I can see it happen using the debugger because the new value is always highlighted in red. As a result when i play the segment i get access violation.


Salem
No Coincidence - I noticed it too. Read below for something even more spooky.

Furthur developments:
After Salem mentioned the "coincedence" i changed the value of EngineStartTimer to 1000. It worked (or didnt break at least). Same for 10.

But, when I tried 101 then Segment[9] became 0x00000065 and when i tried 99 then Segment[7] became 0x00000063.

There is definately a pattern here, just cant figure it out.




 
is cMusicSegment* static?
When a thread starts, the it receives a new copy of all local variables. So, a good idea is to make the cMusicSegment* static.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
ok, so i made it static

static cMusicSegment* Segment[MUSIC_BUFFERS];

no difference (when EngineStartTimer == 100)

"Programmers are tools for converting caffeine into code - BSRF"
 
How big is your
"iTimerID is a global array of int"?

If it is smaller than 101 elements, then of course
iTimerID[EngineStartTimer] = ...
will overwrite some other memory in your program (here Segment[8]). And if EngineStartTimer is 101, then it will overwrite the next memory location (Segment[9]). Etc.

10 is prob. ok if your array is large enough, 1000 is probably overwritting another section of your memory, you just don't know which one yet (very bad!)

I hope this helps,

Vincent
 
Bingo.

I am kicking myself at this moment, here i am thinking that its some multithread thing.

thanks a lot.

have a star.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top