简体   繁体   English

std :: map是否需要初始化?

[英]Does std::map need to be initialized?

I have a singleton, that is an image manager. 我有一个单身人士,那就是图像管理器。 When I try to load an image, I call singleton in this way: 当我尝试加载图像时,我以这种方式调用单例:

utils::CImageBuff* img4 = utils::CImageManager::getInstance()->loadFromFile("uswg.png");

And, into loadFromFile method, I store the image into a map: 然后,将图像存储到map中的loadFromFile方法中:

m_imageMap[imageHashID].setHeight(ilGetInteger(IL_IMAGE_HEIGHT));
m_imageMap[imageHashID].setWitdh(ilGetInteger(IL_IMAGE_WIDTH));
m_imageMap[imageHashID].setBpp(ilGetInteger(IL_IMAGE_BPP));
....

The problem is that I get a mem access error. 问题是我遇到了内存访问错误。 Is like if m_imageMap is NULL o wrong initialized, because excepttion is thrown in the first sentence. 就像m_imageMap为NULL或错误初始化一样,因为在第一句中抛出了异常。 But as I said, is a singleton so the map must me initialized. 但是正如我说的,它是一个单例,因此必须对地图进行初始化。 I'm developing in VC++ 2008. It could be a wrong compilation/ .obj file? 我正在VC ++ 2008中进行开发。可能是错误的编制文件/ .obj文件?

This was working well yesterday 昨天工作很好

Some advice? 一些忠告?

Edit: The declaration of m_imageMap is very simple: 编辑: m_imageMap的声明非常简单:

typedef std::map<unsigned int,utils::CImageBuff> t_imageMap;

As additional information, the exception generated is into the line 1643 of the file output.c. 作为附加信息,生成的异常将进入文件output.c的第1643行。

Edit Well, I'm very dissapointed with this case. 编辑嘛,我对这种情况非常失望。 I've changed the code so now the class no longer is a singleton. 我更改了代码,因此现在该类不再是单例。 Now only m_imageMap is static to make all loaded images visible from any place. 现在只有m_imageMap是静态的,以使所有加载的图像都可以在任何地方看到。 But still fails in the same way. 但是仍然以相同的方式失败。 The error is an access violation . 错误是访问冲突 Moreover, if I change all the method code by this code by this: 而且,如果我通过此代码通过此代码更改所有方法代码:

utils::CImageBuff im;
m_imageMap[1] = im;    // fails here
return &im; 

still fails. 仍然失败。 It is like the map is null or locked in any way, or even bad initialized, but I don't know. 就像地图为空或以任何方式锁定,甚至初始化不正确,但我不知道。 Could you helpme? 你可以帮帮我吗?

If you need some more code, I'll paste, but crashing with this three lines simplifies the problem, I think. 如果您需要更多代码,我将粘贴,但是我认为这三行崩溃会简化问题。

Edit: Finally solved Finally I've changed from 编辑:终于解决终于我从

std::map<unsigned int,utils::CImageBuff>

to

std::map<unsigned int,utils::CImageBuff*>

And now it works. 现在就可以了。 Now I simply need to think a method to delete the contents of an static std::map of pointers. 现在,我只需要考虑一种删除静态std :: map指针内容的方法。

Many thanks to all 非常感谢所有人

I'm assuming that m_imageMap is a regular member variable of a class and you are calling this from a member function of that class. 我假设m_imageMap是一个类的常规成员变量,并且您正在从该类的成员函数中调用它。

If that is the case, then you are likely calling that member function on a zombie, null otherwise invalid pointer. 如果真是这样,那么您很可能在僵尸上调用该成员函数,否则为null,否则为无效指针。 Is the line of code with m_imageMap in it the first use of a member variable in that function? 带有m_imageMap的代码行是否是该函数中第一次使用成员变量的代码? That's a pretty common giveaway. 那是很普通的礼物。

The easy way to debug this problem is to look at it in the debugger. 调试此问题的简单方法是在调试器中查看它。 As long as this error happens in a debug build, VC++ will happily catch the access violation exception and break right there at that instant. 只要在调试版本中发生此错误,VC ++就会很高兴地捕获访问冲突异常并立即在该处中断。 Look at the value of the "this" pointer in the local variables watch window. 在局部变量监视窗口中查看“ this”指针的值。 Is it null or 0xcdcdcdcd ? 是null还是0xcdcdcdcd

std::map does not require initialization- other than that provided by the appropriate constructor. std :: map不需要初始化-除了适当的构造函数提供的初始化之外。 As long as you called a constructor (the default one is fine too), then the map is properly initialized. 只要调用了构造函数(默认设置也可以),即可正确初始化地图。 The Singleton is a truly terrible pattern with many, many horrific problems, and you should excise such demons from your code, but none of them normally include this. Singleton确实是一个可怕的模式,有很多很多可怕的问题,您应该从代码中删除这些恶魔,但通常都没有。 You will need to post another question with the Singleton code in it to get help with the actual problem at hand. 您将需要在其中发布带有Singleton代码的另一个问题,以获取有关实际问题的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM