STL Map Scoping Issue

S

sfncook

I have some code that needs to declare an STL Map in the static scope,
insert values into the map in the function of one class and access the
values in the functions of other classes. This seems like a pretty
straight forward request, but when I implement it I get either garbage
data when I try to access it or I get an Access Violation runtime
error (depending on the state of my code while I try to figure out how
to get this rediculously simple idea to work). What the heck?! What
am I doing wrong? Here is some sample code that gives an Access
Violation:

typedef struct _ShipType {
int a;
int b;
int c;
} ShipType;

static std::map<int, ShipType *> shipTypeMap;

void GameLoader::LoadGame()
{
shipTypeMap[0] = new ShipType();
shipTypeMap[0]->a = 1;
shipTypeMap[0]->b = 2;
shipTypeMap[0]->c = 3;
}

int main(int argc, char* argv[])
{
printf("Callback function test.\n");

GameLoader * gl = new GameLoader();

gl->LoadGame();

std::cout<<shipTypeMap[0]->a<<std::endl;//<-----0xC000000005 Access
Violation here

delete shipTypeMap[0];

getchar();

return 0;
}
 
F

Frank Birbacher

Hi!

So far I have no idea what is causing the AV.

delete shipTypeMap[0];

At least you have undefined behaviour here: deleted pointers are
invalid. invalid pointers may not be read or copied, but only be
assigned to or destructed (when going out of scope). But elements of a
map must be copyable. So you should first remove the pointer from the
map (store it in a local variable) and then delete the object it points to.

To avoid this hassle try using boost::shared_ptr.

Frank
 
G

Gianni Mariani

I have some code that needs to declare an STL Map in the static scope,
insert values into the map in the function of one class and access the
values in the functions of other classes. This seems like a pretty
straight forward request, but when I implement it I get either garbage
data when I try to access it or I get an Access Violation runtime
error (depending on the state of my code while I try to figure out how
to get this rediculously simple idea to work). What the heck?! What
am I doing wrong? Here is some sample code that gives an Access
Violation:

typedef struct _ShipType {
int a;
int b;
int c;
} ShipType;

static std::map<int, ShipType *> shipTypeMap;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I can only think that you're seeing this in more than one compilation unit.
void GameLoader::LoadGame()
{
shipTypeMap[0] = new ShipType();
shipTypeMap[0]->a = 1;
shipTypeMap[0]->b = 2;
shipTypeMap[0]->c = 3;
}

int main(int argc, char* argv[])
{
printf("Callback function test.\n");

GameLoader * gl = new GameLoader();

gl->LoadGame();

std::cout<<shipTypeMap[0]->a<<std::endl;//<-----0xC000000005 Access
Violation here

delete shipTypeMap[0];

getchar();

return 0;
}

The code below compiles and runs as expected.


#include <map>
#include <iostream>

typedef struct _ShipType {
int a;
int b;
int c;
} ShipType;

static std::map<int, ShipType *> shipTypeMap;

struct GameLoader
{
void LoadGame();
};

void GameLoader::LoadGame()
{
shipTypeMap[0] = new ShipType();
shipTypeMap[0]->a = 1;
shipTypeMap[0]->b = 2;
shipTypeMap[0]->c = 3;
}

int main(int argc, char* argv[])
{
printf("Callback function test.\n");

GameLoader * gl = new GameLoader();

gl->LoadGame();

std::cout<<shipTypeMap[0]->a<<std::endl;

delete shipTypeMap[0];

getchar();

return 0;
}
 
F

Frank Birbacher

Hi!

I ran the following code. It doesn't crash on my system. I can't think
of a reason why it should crash at the cout line. Anyway, I don't
understand why you need static storage.

#include <iostream>
#include <ostream>
#include <map>
#include <stdio.h>

struct GameLoader
{
void LoadGame();
};

typedef struct _ShipType {
int a;
int b;
int c;
} ShipType;

static std::map<int, ShipType *> shipTypeMap;

void GameLoader::LoadGame()
{
shipTypeMap[0] = new ShipType();
shipTypeMap[0]->a = 1;
shipTypeMap[0]->b = 2;
shipTypeMap[0]->c = 3;
}

int main(int argc, char* argv[])
{
printf("Callback function test.\n");

GameLoader * gl = new GameLoader();

gl->LoadGame();

std::cout<<shipTypeMap[0]->a<<std::endl;//<-----0xC000000005
AccessViolation here

delete shipTypeMap[0];

getchar();

return 0;
}

Frank
 
M

Markus Schoder

delete shipTypeMap[0];

At least you have undefined behaviour here: deleted pointers are
invalid. invalid pointers may not be read or copied, but only be
assigned to or destructed (when going out of scope). But elements of a
map must be copyable. So you should first remove the pointer from the
map (store it in a local variable) and then delete the object it points
to.

Must say I did not realise that until now. That is unbelievably ugly.

Using the following should be safe though:

template<class T> void deleteAndZero(T *&p)
{
delete p;
p = NULL;
}
 
J

James Kanze

So far I have no idea what is causing the AV.
(e-mail address removed) schrieb:
delete shipTypeMap[0];
At least you have undefined behaviour here: deleted pointers are
invalid. invalid pointers may not be read or copied, but only be
assigned to or destructed (when going out of scope). But elements of a
map must be copyable. So you should first remove the pointer from the
map (store it in a local variable) and then delete the object it points to.

That's in theory. In practice, of course, there's no problem on
any real architecture; no implementation of std::map will read
or copy the pointer unless you actually try to access it.
To avoid this hassle try using boost::shared_ptr.

Sounds like added complexity for no gain.
 
S

sfncook

Wow! You guys are great! Thanks for the responses so far. However, I
forgot to mention one factor that must make the difference: The code
is broken up into different files. I copied the code you had and ran
it fine from a single file as well. However when I divide the code
into different files (as is below) then I get the AV. Thanks a
million for the help so far. Please teach me oh wise sages.

********* typedefs.h *****************
#include "stdlib.h"
#include <map>
#include <vector>

typedef struct _ShipType {
int a;
int b;
int c;
} ShipType;

static std::map<int, ShipType *> shipTypeMap;


************** GameLoader.h ********************
#pragma warning (disable : 4786) //This is a fix for a Microsoft
acknowledged bug.

#include "typedefs.h"

class GameLoader
{
private:
public:
GameLoader(){};
virtual ~GameLoader(){};
void LoadGame();
};

************ GameLoader.cpp *********************
#include "GameLoader.h"

void GameLoader::LoadGame()
{
shipTypeMap[0] = new ShipType();
shipTypeMap[0]->a = 1;
shipTypeMap[0]->b = 2;
shipTypeMap[0]->c = 3;
}


****************** MainFile.cpp *********************
#include "GameLoader.h"

#include <stdlib.h>
#include <iostream>
#include <map>

int main(int argc, char* argv[])
{
printf("Callback function test.\n");

GameLoader * gl = new GameLoader();

gl->LoadGame();

std::cout<<shipTypeMap[0]->a<<std::endl;

delete shipTypeMap[0];

getchar();

return 0;
}

^^^^^^^^^ FIN ^^^^^^^^^^^^^^^
 
F

Frank Birbacher

Hi!

Wow! You guys are great! Thanks for the responses so far. However, I
forgot to mention one factor that must make the difference: The code
is broken up into different files.

Indeed that is the difference, as Gianni already pointed out. :)
********* typedefs.h *****************
#include "stdlib.h"
#include <map>
#include <vector>

typedef struct _ShipType {
int a;
int b;
int c;
} ShipType;

static std::map<int, ShipType *> shipTypeMap;

This definition of shipTypeMap includes "static". That makes shipTypeMap
appear mutiple times: once for each translation unit (= cpp file). So
your initialisation code initialises one instance while the other code
accesses the uninitialised one => oops. You need to do the following:

typedefs.h:
extern std::map<int, ShipType*> shipTypeMap;

somewhere.cpp:
std::map<int, ShipType*> shipTypeMap;

This makes one global instance of shipTypeMap which is then access from
all code.

HTH,
Frank
 
F

Frank Birbacher

Hi!

James said:
That's in theory. In practice, of course, there's no problem on
any real architecture; no implementation of std::map will read
or copy the pointer unless you actually try to access it.

Yes, no problem in practise.
Sounds like added complexity for no gain.

Sounds like added memory security for little effort as well. Even if it
is of little help with uninitialised pointers it is of great help for
managing memory (less leaks) and exception safety.

Frank
 
B

BobR

Just a couple of *suggestions*....
( I think Frank nailed your problem.)
********* typedefs.h *****************
#include "stdlib.h"

Is "stdlib.h" a file you wrote? If you intended the standard stdlib, you
should use:

#include <stdlib.h>

.... or the C++ version:

#include said:
#include <map>
#include <vector>

Remove that said:
typedef struct _ShipType {

An underline followed by an uppercase letter is reserved to implementation.
I'd use something else there ( like Ship_Type).
int a;
int b;
int c;
} ShipType;

Why not just:

struct ShipType{
int a;
int b;
int c;
};

[snip]
#include "typedefs.h"

class GameLoader{

Ok if you need the documentation, but, 'class' defaults to 'private', so,
it's not required.
public:
GameLoader(){};
virtual ~GameLoader(){};
void LoadGame();
};

************ GameLoader.cpp *********************
#include "GameLoader.h"

void GameLoader::LoadGame(){
shipTypeMap[0] = new ShipType();
shipTypeMap[0]->a = 1;
shipTypeMap[0]->b = 2;
shipTypeMap[0]->c = 3;
}

If you add a constructor to your struct:
struct ShipType{
int a;
int b;
int c;
ShipType( int aa, int bb, int cc) : a(aa), b(bb), c(cc){}
// ShipType() : a(0), b(0), c(0){} // might also want this
}; // note: no longer a POD type

.... then you could do:

void GameLoader::LoadGame(){
shipTypeMap[0] = new ShipType( 1, 2, 3 );
}
****************** MainFile.cpp *********************
#include "GameLoader.h"

#include <stdlib.h>
#include <iostream>
#include <map>

int main(int argc, char* argv[])

If you are not using the command line args:

int main()
{
printf("Callback function test.\n");
GameLoader * gl = new GameLoader();
gl->LoadGame();
std::cout<<shipTypeMap[0]->a<<std::endl;
delete shipTypeMap[0];
getchar();
return 0;
}
 
B

Bo Persson

Markus Schoder wrote:
:: On Sun, 12 Aug 2007 09:38:40 +0200, Frank Birbacher wrote:
::: (e-mail address removed) schrieb:
:::: delete shipTypeMap[0];
:::
::: At least you have undefined behaviour here: deleted pointers are
::: invalid. invalid pointers may not be read or copied, but only be
::: assigned to or destructed (when going out of scope). But elements
::: of a map must be copyable. So you should first remove the pointer
::: from the map (store it in a local variable) and then delete the
::: object it points to.
::
:: Must say I did not realise that until now. That is unbelievably
:: ugly.

But that is also the way some hardware work(ed). Loading a pointer
into an address register might involve verifying access rights and/or
validity.

On a segmented system, like Windows 286 with 64 kB per segment,
deleting an entire segment might make the runtime release the segment
to to OS, which promptly removed the segment from the descriptor
table. Now, loading that pointer into a segment register would case a
missing segment trap.


This segment hardware is still present in 32 bit x86 processors,
though we don't use it much. The language rules are there to allow its
use, if anybody should want to!

::
:: Using the following should be safe though:
::
:: template<class T> void deleteAndZero(T *&p)
:: {
:: delete p;
:: p = NULL;
:: }


Yes.


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top