| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
12 Aug 2007 01:22:44 AM |
| Object: |
STL Map Scoping Issue |
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;
}
.
|
|
| User: "Gianni Mariani" |
|
| Title: Re: STL Map Scoping Issue |
12 Aug 2007 02:53:34 AM |
|
|
wrote:
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;
}
.
|
|
|
|
| User: "Frank Birbacher" |
|
| Title: Re: STL Map Scoping Issue |
12 Aug 2007 02:54:00 AM |
|
|
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
.
|
|
|
|
| User: "Frank Birbacher" |
|
| Title: Re: STL Map Scoping Issue |
12 Aug 2007 02:38:40 AM |
|
|
Hi!
So far I have no idea what is causing the AV.
sfncook@gmail.com 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.
To avoid this hassle try using boost::shared_ptr.
Frank
.
|
|
|
| User: "James Kanze" |
|
| Title: Re: STL Map Scoping Issue |
12 Aug 2007 10:46:35 AM |
|
|
On Aug 12, 9:38 am, Frank Birbacher <bloodymir.c...@gmx.net> wrote:
So far I have no idea what is causing the AV.
sfnc...@gmail.com 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 t=
o=2E
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.
--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
|
|
|
| User: "Frank Birbacher" |
|
| Title: Re: STL Map Scoping Issue |
12 Aug 2007 12:25:39 PM |
|
|
Hi!
James Kanze schrieb:
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.
To avoid this hassle try using boost::shared_ptr.
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
.
|
|
|
| User: "" |
|
| Title: Re: STL Map Scoping Issue |
12 Aug 2007 12:42:39 PM |
|
|
Why don't I visit this site more often? You guys are great. Thank a
million.
.
|
|
|
|
|
| User: "" |
|
| Title: Re: STL Map Scoping Issue |
12 Aug 2007 12:16:09 PM |
|
|
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 ^^^^^^^^^^^^^^^
.
|
|
|
| User: "BobR" |
|
| Title: Re: STL Map Scoping Issue |
12 Aug 2007 02:27:18 PM |
|
|
<sfncook@gmail.com> wrote in message...
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 <cstdlib> // you may need to add 'std::' to the 'calls'.
#include <map>
#include <vector>
Remove that <vector> header if you are not using it.
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{
private:
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;
}
--
Bob R
POVrookie
.
|
|
|
| User: "" |
|
| Title: Re: STL Map Scoping Issue |
12 Aug 2007 05:10:13 PM |
|
|
Thank you! Very constructive comments that I will take to heart.
.
|
|
|
|
|
| User: "Frank Birbacher" |
|
| Title: Re: STL Map Scoping Issue |
12 Aug 2007 12:22:48 PM |
|
|
Hi!
sfncook@gmail.com schrieb:
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
.
|
|
|
|
|
|
| User: "Markus Schoder" |
|
| Title: Re: STL Map Scoping Issue |
12 Aug 2007 10:21:14 AM |
|
|
On Sun, 12 Aug 2007 09:38:40 +0200, Frank Birbacher wrote:
sfncook@gmail.com 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.
Using the following should be safe though:
template<class T> void deleteAndZero(T *&p)
{
delete p;
p = NULL;
}
--
Markus Schoder
.
|
|
|
| User: "Bo Persson" |
|
| Title: Re: STL Map Scoping Issue |
15 Aug 2007 06:33:02 AM |
|
|
Markus Schoder wrote:
:: On Sun, 12 Aug 2007 09:38:40 +0200, Frank Birbacher wrote:
::: 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
.
|
|
|
|
|
|

|
Related Articles |
|
|