Hello,i am writing an application which involves invoking multiple threads to pick data from a queue and then delete or pop the retrieved data.My codes work well for this purpose but then my application suddenly crashes with a dialog with the following error message:

Debug assertion failed!

Program:....

Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
------------------------------------------------------------------------------

My codes:

#include <string>
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <iterator>
#include <boost/regex.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>
#include <boost/bind.hpp>
#include <ctime>
#include <cstdlib>
#include <boost/ref.hpp>
#include <boost/thread/mutex.hpp>

/*
* This class is responsible for the creation of multi-thread parses for simultaneous parsing processes.
*/

using namespace std;


class Parser
{
private:
	queue<string>* Pages_Queue;   //Holds the a pointer to the unparsed pages queue
	int nParser;              //Holds the number of parsers to work with
	int PID;						//The parser id
	boost::thread_group threads;
	boost::mutex mutex_;


	
public:
	//Constructor
	Parser(int pid,int nparser=10)
	{
		this->nParser=nparser;
		this->PID=pid;
	}

	//Destructor
	~Parser(){};

	/*
	*Loads the unparsed pages queue
	*/
	bool LoadPages(queue<string>* p_queue)
	{
		Pages_Queue=p_queue;
		return true;
	}

	/*
	* Returns the number of pages in the queue
	*/
	int GetQueueSize() const
	{
		return Pages_Queue->size();
	}
	
	/*
	* Sets the number of parses to work with
	*/
	bool SetNumParser(int num)
	{
		this->nParser=num;
		return true;
	}

	/*
	* Returns the number of parsers 
	*/
	int GetNumParser()
	{
		return this->nParser;
	}

	void Start(int id)
	{
		
		while(true)
		{
			try
			{
			if(Pages_Queue->size() > 0)
			{

			//boost::posix_time::seconds workTime(5);  
			//boost::this_thread::sleep(workTime); 
			//boost::mutex::scoped_lock lock(mutex_);
			string page=Pages_Queue->front();
			cout << "THREAD: " << id << endl << "Queue Size: " << Pages_Queue->size() << endl << page << "\n\n\n\n\n\n\n";
			//Pages_Queue->pop();
			}
			else
			{
				cout << "\n\n FINISHED";
				break;
			}
			}
			catch(...)
			{
				cout << "\n\n\nUnknown error";
			}
		}
	}

	/*
	* Creates a new child parser to act on queue.
	*/
	bool Parse(int id)
	{
		//Creating pointer to Start function
		//typedef void(Parser::*Start_Prt)(int a);
		//Start_Prt prt_s=&Parser::Start;
		//Create the thread and pass the Start pointer to the thread

		time_t now=time(0);
		tm* localtm2 = localtime(&now);
		
		cout << "Child parser created: " << id << " at " << asctime(localtm2) << endl;
		//boost::thread pThread(boost::bind(boost::mem_fn(&Parser::Start),this,id));
		threads.create_thread(boost::bind(boost::mem_fn(&Parser::Start),this,id));
	
		

		return true;
	}

	/*
	* Begins the parsing process
	*/
	bool Begin()
	{
		for(int i=0; i<this->nParser; i++)
		{
			this->Parse(i);
		}
		boost::thread::yield();
		threads.join_all();

		return true;
	}

};

Its a little messy though.Please any help will be appreciated

Thanks

Recommended Answers

All 3 Replies

Put simply, you trashed the memory pool, and that assertion pops up to tell you that it has happened.

Without a COMPLETE program which crashes to study, there's not a lot to suggest from over here.

If you run the code in the debugger, the debugger will catch the assert, so at least you can find out where the problem is triggered.

But that in itself won't tell you where the problem is CAUSED.

You're not using any horrid char* arrays in your C++ program are you?
Or any slightly less horrid new/delete ?

>>queue<string>* Pages_Queue
One possibility is the way you are using this pointer. The Parser class does not own the memory for this pointer, its just passed into one of the methods. Consequently the pointer could get deleted or reused somewhere else and Parser class would be none the wiser. IMO that is a dangerous situation. It would be safer for LoadPages() to copy (duplicate) that queue rather than just using the same pointer so that it has its own copy.

All right, thanks guys.I will try all suggestions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.