开发者

Access Violation C++

I am new to c++, and I'm facing the Access Violation exception. The problem is as follows, I have backend.h file which contains the struct

struct Config
{
 ....
}conf;
get开发者_StackOverflow中文版_conf();

The implementation in backend.cpp

Config get_conf()
{
return conf;
}

backend.cpp and backend.h are compiled and built to generate a dll. This dll is included in a VS solution that way

Config config = get_config();
Config *g_config = &config;

The code compiles and run fine but it throws an exception when the function is called

First-chance exception at 0x75d6d36f in BA.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x002ec494..
First-chance exception at 0x75d6d36f in BA.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x002ebe28..
First-chance exception at 0x75d6d36f in BA.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..
First-chance exception at 0x77962d37 in BA.exe: 0xC0000005: Access violation reading location 0x41ce5ab2.
Unhandled exception at 0x77962d37 in BA.exe: 0xC0000005: Access violation reading location 0x41ce5ab2.
The program '[4624] BA.exe: Native' has exited with code -1073741819 (0xc0000005).

I think the problem has something to do with pointers & struct please help me out


The question does not make much sense, and it indicates that you should probably start with a good book or tutorial to understand the basics (declaring a variable in a header file is probably not a good idea, the function declaration is wrong in the header), the whole terminology you are using is strange...

Anyway, as of your particular problem now, the best advice is to run the application inside a debugger, and you will see what is going on. My first guess (looking at a crystal ball) is that you are creating a Config object with automatic storage duration in a scope (Config config = get_config(); --this creates a new object in the current scope that is a copy of the conf object) and then obtaining a pointer to it (Config *g_config = &config; -- pointer to config object in the current scope, not to global conf) that you then pass around and later use outside of the scope when the object has already been destroyed.

Another option is that you are facing the initialization order fiasco, if those two lines are not inside a context, in which case, you could be trying to copy a yet uninitialized conf object into config.

The std::bad_alloc itself is an exception that is thrown when new fails to allocate memory, and it can happen if the system is out of resources (probably not the problem here) or if the request is incorrect.


If you have this declarations in the header:

struct Config
{
 ....
}conf;
get_conf();

then it is likely to be equivalent to:

struct Config { ... } ;
Config conf;
int get_conf();

"likely", because in C omitting the return type implied that the return type is int. This is not the case in C++, but MSVC's compiler isn't really so standards-compilant so it might have missed that, esp. if you have warning disabled.

If so, this may cause the declaration

int get_conf();

to be linked with definition:

Config get_conf() { ... }

in the source file. If this links, such call would crash your program.


One problem might be with the construction of the structure, it might go out of order when you are compiling i.e before the structure is created/initialized , you are assigning some address to the pointer.

So, make sure all the GLOBAL data creation/initialization happens in the same cpp compilation unit


I don't have a compiler to confirm, but this is at least part of the problem. conf defined in the header won't work. If you used the header in two cpp files you would get duplicate symbols.

I would guess that Kos is correct in the why you get the message you do with the seg fault. You may have some warnings (don't disable them) that would confirm that.

My other guess would be that it may be trying to use a declared but uninitialized variable.

This is what I would expect, assuming you don't want to make a bunch of copies of your Config object.

// .h
struct Config
{
  // ...
};
Config &get_conf();

// .cpp
Config &get_conf()
{
  static Config conf;
  return conf;
}

// some other file
Config &conf = get_conf();
conf.value = 5;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜