• Advertisement
Sign in to follow this  

Problem with std:map

This topic is 4526 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to insert an element into a std:map and I'm getting a crash. Can anybody tell me what I'm doing wrong? I've been looking at it for a few hours and I'm no closer to solving it (google isnt much help either.)

LONG idWorkflow = 1;
QUEUES Queue;

m_Workflow.insert(WORKFLOW::value_type(idWorkflow, Queue));

The insert blows up. Here is my entire code:

// This holds the Queues
typedef map<LONG, LONG> QUEUE; /* idQueue, QUEUEINFO */
typedef struct
{
  QUEUE Queue;
} QUEUES;

// This holds X number of Queues in workflows.
typedef map<LONG, QUEUES> WORKFLOW; /* idWorkflow, QUEUES */

//----------------------------------------

WORKFLOW    m_Workflow;
LONG        idWorkflow = 1;
QUEUES      Queue;

m_Workflow.insert(WORKFLOW::value_type(idWorkflow, Queue));


I also tried to remove the struct QUEUES and use QUEUE directly but that didn't help much at all either. Any ideas?

Share this post


Link to post
Share on other sites
Advertisement
Are you putting stuff into the map in a constructor of an object thats declared as a static object? If this is the case your object might be getting constructed and trying to add to a map that hasn't been constructed yet.

Share this post


Link to post
Share on other sites

#include <map>
using std::map;

// This holds the Queues
typedef map<long, long> QUEUE; /* idQueue, QUEUEINFO */
typedef struct
{
QUEUE Queue;
} QUEUES;

// This holds X number of Queues in workflows.
typedef map<long, QUEUES> WORKFLOW; /* idWorkflow, QUEUES */

//----------------------------------------

int main( int argc, char* argv[] )
{
WORKFLOW m_Workflow;
long idWorkflow = 1;
QUEUES Queue;

m_Workflow.insert(WORKFLOW::value_type(idWorkflow, Queue));

return 0;
}



That does not crash for me at all, so you have a logical error in your program elsewhere.

Share this post


Link to post
Share on other sites
Drew:

Thanks.
I just did a test with your example and it works fine here too.
I'll keep looking.

Krum:

Nope

Fruny:

Yep, hits the catch on a try() catch.

Share this post


Link to post
Share on other sites
Quote:
Original post by JoeyBlow2
Fruny:

Yep, hits the catch on a try() catch.


What exception?

Share this post


Link to post
Share on other sites
Unhandled exception at 0x0045e78b in OSS.exe: 0xC0000005: Access violation reading location 0x00000004.

Share this post


Link to post
Share on other sites
I just found out if I declare WORKFLOW variable local to where I do the insert it doesn't crash, but if I make it global or a private member of the class then it does.

Share this post


Link to post
Share on other sites
That's not a C++ exception. It means you have a NULL pointer somewhere. Guessing from the 4 bytes offset, you are probably accessing the second member of a structure or second element of an array through a pointer, and the first is an int or similar-sized variable. I strongly doubt it is related to your map insertion.

You might also have an array overrun on a variable that precedes the map and are corrupting the map's internal data (e.g. overwriting it with zeroes).

Share this post


Link to post
Share on other sites
Ok I got it to work by moving the initializing function into the class constructor rather than calling it during the ATL initializing section. There must be a conflict with ATL somehow, or the ATL function I'm initializing from.

Thanks for the help guys. I get it straighened up from here. I thought I was using STL incorrectly.

Share this post


Link to post
Share on other sites
Spordic issues like this are usually caused by overwriting array bounds or otherwise accessing memory incorrectly.

Moving variable declarations and function calls around can appear to solve the problem as this can change the memory layout of variables.

It is very unlikely that you have actually solved the problem. Although it works now, it is likely to recur sporadically as you change your code until you find the root cause of the problem.

Are you compiling in release or debug mode? If it's in debug, try compiling in release to see if the problem reappears.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement