Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

In a
C++
stack<std::string>
I am doing a push() operation and stack contains data as below

C++
[0]"-0585,-2857,-0059,00359,00360,00360,00100,02000,-0030,00360,00360,00360,00360,00360,00360,A%"
[1]"-0457,-2004,-0059,00359,00360,00360,00100,02000,-0030,00360,00360,00360,00360,00360,00360,B%"
[2]"-0477,-0358,-0059,00359,00360,00360,00100,02000,-0030,00360,00360,00360,00360,00360,00360,C%"
[3]"-0128,-1538,-0059,00359,00360,00360,00100,02000,-0030,00360,00360,00360,00360,00360,00360,D%"
[4]"-0155,-0378,-0059,00359,00360,00360,00100,02000,-0030,00360,00360,00360,00360,00360,00360,E%"


and so on 10000+.

Simultaneously I'm also performing a top() operation, which should return
C++
"-0155,-0378,-0059,00359,00360,00360,00100,02000,-0030,00360,00360,00360,00360,00360,00360,E%"


but it returns the following as string format
C++
"-1.#IND,-1.#IND,-1.#IND,00359,00360,00360,00100,02000,-0030,00360,00360,00360,00360,00360,00360,E%"


Moreover if i do check using
stack.top().size() != 0
,the top value changes and returns -1.#IND.....so on.

Why is the first 15 elements(excluding the ',') is changing to this nan values in a string?

//improved
I am using a boost client-server architecture(simple chat example).
Client1 ---posts data--> Server --publish data --> Client2(reads data in stack).
Client1 reads data from a COM port and posts the data.
Client2 pushes the received data to the stack.
The stack contents remains okay. But whenever i use the following code problem appears
C++
boost::shared_lock<boost::shared_mutex> lock(_access);		
std::string mStr = "";
if(!stackstring.empty() && stackstring.top().size() != 0)
{
   mStr = stackstring.top();
   stackstring.pop();	
}


only the top value gets corrupted with -1.#IND..
so mStr gets wrong values. boost locks are used as read/write operation occurs simultaneously on the same stack variable.


Any help regarding this problem is appreciated.

Thanks and Regards,
Sayan.
Posted
Updated 3-Oct-13 23:21pm
v3
Comments
Richard MacCutchan 4-Oct-13 4:11am    
This is most unlikely, the stack class will not modify the content of any element. Please use the Improve question link and show the actual code you are using.
Richard MacCutchan 4-Oct-13 5:42am    
Something else is corrupting this data, it has nothing to do with the stack class. I can reproduce the basic stack, push these strings in and pop them back out and they are exactly the same.
Sayan Bera 4-Oct-13 6:16am    
Can this happen because of time synchronization or COM port?
I have done a few tests, then I found that if a post data by generating it in Client1 not getting values from the COM port, then this corruption of data doesn't happen.
Richard MacCutchan 4-Oct-13 6:25am    
There could be many reasons, but you will have to use your debugger to track it down. It looks like some activity is generating these bad numbers, and overwriting a stack entry without using the lock properly.
Sayan Bera 4-Oct-13 6:41am    
Thanks Mr. Richard.
Yes there could be many reasons, but I'll first give a try with this lock.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900