Expression: deque iterator not dereferencable

Hi together,

i have a problem with using a queue of vector objects. Basically all i do is create a new object, put it in the queue and right after that try to read it.

I put it in the queue with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void ProcessData::getData(CharVectorPtr& buffer, std::mutex* inBufferMutex){
	std::vector<char16_t> temp;
	printf("processing activ\n");
	while(1){
		if(buffer->size()>HEADERSIZE){ //check if there is data in the message ( more than just the header )

			inBufferMutex->lock();
			workBufferMutex->lock();
			temp.insert(temp.begin(),buffer->begin(),buffer->end());
			workBuffer.push(std::vector<char16_t>(temp)); //copy data from buffer
			printf("size %d\n",workBuffer.size());
			buffer->clear();
			workBufferMutex->unlock();
			inBufferMutex->unlock();

			threads.push_back(ThreadPtr(new std::thread(&ProcessData::process, this)));


		}
	}
	printf("processing stopped\n");
}


Note: The size changes to 1. So i figured the element was created and added.

I remove it with:
1
2
3
4
5
6
7
8
9
10
11
12
void ProcessData::process(){
	std::vector<char16_t> temp;

	workBufferMutex->lock();
	if(!workBuffer.empty()){
		temp = workBuffer.front();
		workBuffer.pop();
	}
	workBufferMutex->unlock();

	calculate(temp);
}


The error occures while calling workBuffer.front() .
workBuffer is initialized with
 
	std::queue<std::vector<char16_t>> workBuffer;


All it says is "File: G:\Anwendungen\Visual Studio 2012\VC\include\deque
Line: 331

Expression: deque iterator not dereferencable"

I am at loss here....

PS: The idea is to copy the data out of buffer ( filled from another thread ) into workBuffer before starting to analyze the data, so the buffer can be refilled again.
Each workerthread is then given the workBuffer for dequeing, so that it can start working while the queue can be refilled by the thread running the while(1) loop.

mfg
Maggistro
Last edited on
I worked on this again today and did the following:

- removed threaded call to process
- switched from object queue to pointer queue
- tried to call only workBuffer.front() without using the return value
- made a testprogram with just the queue to see if i use it correctly ( seems i do )

Then i tried to change the container of the queue to list.
This led to "Access Violation on reading position 0xFFFFFFFFFFFFFFFFF" on calling workBuffer.size() in line 11.

So i figured something must go wrong initializing the queue and its container, but i cant see what...
Last edited on
1
2
void ProcessData::process(){
	std::vector<unsigned short> temp; // unsigned short -> char16_t? 


- switched from object queue to pointer queue
That's not a good idea. You will have a pointer to a local variable.

By the way:
workBuffer.push(std::vector<char16_t>(temp)); // No need to copy twice
Hi coder777,

char16_t is the same as unsigned short ( 16bit positive number ). But thanks for the note,
adapted the code.

If i push an object, is it automatically duplicated?
If not i would work on temp, which could then be changed in the while loop even before my threaded ProcessData::process() function reads it.


char16_t is the same as unsigned short ( 16bit positive number ).
No it's not. Your configuration might coincidentally leads to it

If i push an object, is it automatically duplicated?
Yes. So you can even write this:
workBuffer.push(*buffer); // workBuffer will contain a copy of buffer

If not i would work on temp, which could then be changed in the while loop even before my threaded ProcessData::process() function reads it.
This would only happen when you pass a pointer to temp

Is buffer also used in different threads? If so then at least line 5 in getData() is a problem

1
2
void ProcessData::getData(CharVectorPtr& buffer, std::mutex* inBufferMutex){
// CharVectorPtr& buffer -> why do you have a reference to a pointer? 
Ok, char16_t is to be interpreted as a character, unsigned short is the number.
But i only need my vector elemets to be 16bit long.

Thanks for that information on .push(), i was not aware of that. I adapted my code.

Buffer is the main buffer wich is filled in another thread. It is defined as :
typedef std::shared_ptr<std::vector<char>> CharVectorPtr;

I am aware that i need to do some "casting" on the binary level to get from char to char16_t, but that is another issue.

buffer-> size()
Gives access to the size of the std::vector<char> object.

The threading does not cause a problem here, since this is only a reading access.
Basically what i do is check the size of buffer, and as soon as i notice something is received, i wait for the receiving thread to end with the bufferMutex.lock(), so i can analyze the header and copy all data from one package.
Last edited on
Ok i fixed the issue.

I just changed the queue to an object pointer and specifically initialized it with a new object.
Using that pointer the queue worked.
Dont get why, but it does....
Topic archived. No new replies allowed.