Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
2464 Discussions

Access violation using "insert" on concurrent_unordered_map

FredP
Beginner
941 Views
Trying to store a key, value pair in a concurrent_unordered_map which needs concurrent insertion capabilities.
The following code is the function that is failing at run-time with an Access Violation error:
[cpp]void EEconomy::CreateProductNetwork( std::vector<:NETWORKENTRY> entries )
{
	//scroll through each item in the list and build the network
	for(std::vector<:NETWORKENTRY>::const_iterator i = entries.cbegin(); i != entries.cend(); ++i)
	{
		EEconomy::networkEntry currentEntry = *i;
		EProductClass* product = new EProductClass(currentEntry.productID, currentEntry.productName, currentEntry.ancestors, currentEntry.successors);

		std::pair ins(currentEntry.productID, product);

		this->productMap.insert(ins);

	}

}[/cpp]
The last line is the failing one. In the EEconomy header file, the productMap definition is:
[cpp]	concurrent_unordered_map productMap;[/cpp]
No problem building the code, but I'm obviously making a mistake. Unfortunately, I can't work it out from the reference guide and there seem to be no examples. What (probably obvious) mistake am I making?
Fred
0 Kudos
4 Replies
Anton_M_Intel
Employee
941 Views
Fred, the code from your post looks fine. Probably, the problem lurks somewhere else. How do you use the map before this insertion? How productMap is initialized (any constructor or rehash() arguments)? Could you say where exactly access violation occurs inside TBB's code? Can you see the stack? Also, what version of TBB do you use? What's compiler version?
0 Kudos
FredP
Beginner
941 Views
I suspect this is where I'm going wrong, and was uncomfortable about this beforehand!
The productMap is unused beforehand, and I don't use any constructors for it - the declaration is as in my post above, and the code snippet is its first use. I'm using TBB v3 Update 5, and VC++ 2010.
The violation occurs in:
> WorldSim.exe!__TBB_machine_load_store<__int64,8>::load_with_acquire(const __int64 & location) Line 553 + 0x5 bytes C++
which traces back through a series of additional TBB calls to my code.
I suspect my failure is in initialising the unordered_map, but if so, can someone give me a simple code snippet for using the container, and I'll adapt it back to my use case.
For convenience, the entire call stack is:
> WorldSim.exe!__TBB_machine_load_store<__int64,8>::load_with_acquire(const __int64 & location) Line 553 + 0x5 bytes C++
WorldSim.exe!__TBB_load_with_acquire<__int64>(const __int64 & location) Line 604 C++
WorldSim.exe!tbb::internal::atomic_impl::operator unsigned __int64() Line 201 + 0xa bytes C++
WorldSim.exe!tbb::interface5::internal::concurrent_unordered_base<:INTERFACE5::CONCURRENT_UNORDERED_MAP_TRAITS>,std::equal_to >,tbb::tbb_allocator<:PAIR> >,0> >::internal_insert(const std::pair & value) Line 1110 + 0x14 bytes C++
WorldSim.exe!tbb::interface5::internal::concurrent_unordered_base<:INTERFACE5::CONCURRENT_UNORDERED_MAP_TRAITS>,std::equal_to >,tbb::tbb_allocator<:PAIR> >,0> >::insert(const std::pair & value) Line 856 + 0x14 bytes C++
WorldSim.exe!EEconomy::CreateProductNetwork(std::vector<:NETWORKENTRY> > * entries) Line 43 + 0x26 bytes C++
fred
0 Kudos
Anton_M_Intel
Employee
941 Views
So, it fails on the first read of a plain member of the container class (atomic "my_number_of_buckets"). It means that 'this' pointer is wrong, and the problem does not relate to TBB. It seems like CreateProductNetwork() is called via bad (null) pointer to EEconomy class.
0 Kudos
FredP
Beginner
941 Views
That's great - it's because EEconomy is a Singleton and it transpired that I'd screwed up the initialisation. I was uncertain though, because I was worried I'd misused the container, so tried debugging that first.
Sorry if this wasted your time (such a simple error) - but it helped me a great deal!
Fred
0 Kudos
Reply