vector<Object *> use 和 new Object() 的 C++ 编程问题

标签 c++ oop pointers vector sdl-2

我现在真的卡住了...

总结:

我的 C++ 应用程序每次都崩溃

Access Violation Reading Error on location 0x000000008

这肯定是使用多个对象指针 vector 的错误 使用 new 语句创建对象的位置。

----------------

我想通过使用 SDL2 创建一个简单的 GUI 引擎来训练我的技能 并设计成这样:

使用外部(全局)创建引擎类/对象

因为我想打包所有与引擎相关的东西,比如更新功能 在一个数据包中并通过初始化函数初始化它:

使用 extern 创建 GUI 类/对象

我希望它也能在一个“包”中并在这个类中 我有多个 std::vector <#PointerToObject#> 要保留 我创建的窗口对象在全局堆上存活 临时创建它们是正确的还是有更好的 这样做的方法?

图形界面.h

//...
class GUIWindow
{
    //...
    std::vector< GUIGadgetLable* > _guiGadgetLables;
    std::vector< GUIGadgetButton* > _guiGadgetButtons;
    //...
};
class GUI
{
    //...
    std::vector< GUIWindow* > _guiWindows;
    //...
    void createWindow(std::string title, int x, int y, int sx, int sy);
};
//...
extern GUI engineGUI;

图形用户界面.cpp

//...
void GUI::createWindow(std::string title, int x, int y, int sx, int sy)
{
    this->_guiWindows.insert(this->_guiWindows.end(), new GUIWindow(title,x,y,sx,sy));
    return this->_guiWindows.at(this->_guiWindows.size()-1);
}
//...
GUI engineGUI;

我在许多对象中都这样做,我创建 GUI 的历史是这样的:

界面

1>GUI窗口

2->GUIGadgetLable

3-->GUI文本

4--->ResourceTexture(createFromText)

2->图形用户界面图像

3-->ResourceTexture(createFromFile)

2->GUIGadgetButton

3-->GUI文本

一切正常,直到我实现我的 ResourceManager 到胶囊 SDL 纹理渲染功能,例如将位图从文件加载到 SDL_Texture 并从文本创建纹理。

资源管理器.h

class ResourceFont
{
public:
    TTF_Font* _font = NULL;
    int _fontSize = 3;
    std::string _fontFile = "";

    bool loadFromFile(std::string file, int fSize);

    void destroy();
};

class ResourceTexture
{
public:
SDL_Texture *_texture = NULL;

    SDL_Rect _size;
    SDL_Rect _position;

    std::string _data = ""; //File/Text
    bool _visible = true;

    bool _isText = false;
    int _fSize = 3;
    SDL_Color _textColor = {70,30,120};

    bool createFromFile(std::string file, int x, int y, float sx, float     sy);
    bool createFromText(std::string text, int fSize, int x, int y,  SDL_Color foreGround);
    bool changeText(std::string text);

    void resize(float sx, float sy);

    void update(int x, int y);
    void draw();
    void destroy();
};

class ResourceManager
{
public:
    std::vector< ResourceFont* > _resourceFonts;
    std::vector< ResourceTexture* > _resourceTextures;

    TTF_Font* addFont(std::string fontFile, int fSize);
};

extern ResourceManager engineResource;

Texture 的东西完全工作,但是在 ResourceFont* Vector 和 addFont 函数(它使用 TTF_Font 为 ResourceTexture::createFromText 函数创建一个新的字体句柄)的实现之后,它在程序下面崩溃并出现访问冲突读取在地址 0x0...08,这在我调用

new ResourceFont()

return this->_resourceFonts.at(i)->_font;

在我的代码中,所以我认为我的堆已损坏,但我不知道为什么在此之前使用相同方法的所有东西都运行良好?

资源管理器.cpp

TTF_Font *ResourceManager::addFont(std::string fontFile, int fSize)
{
    std::cout << "GOIN "<< fSize << std::endl;
    if (this->_resourceFonts.size())
    {
        for (unsigned int i=0; i < this->_resourceFonts.size(); i++)
        {
            std::cout << "Compare: " << i << " Search: " << fSize << " Found: " << this->_resourceFonts.at(i)->_fontSize << std::endl;
            if (this->_resourceFonts.at(i)->_fontSize==fSize)   //this->_resourceFonts.at(i)->_fontFile==fontFile&&
            {
                //std::cout << this->_resourceFonts.at(i) << std::endl;
                if (this->_resourceFonts.at(i)->_fontSize==fSize)
                {
                    std::cout << "Found one " <<    &this->_resourceFonts.at(i)->_font << std::endl;
                    return this->_resourceFonts.at(i)->_font; //Crashes sometimes here were it try to acces the object
                }
                else
                {
                    return 0;
                }
            }
            std::cout << "Nope" << std::endl;
        }
    }
    std::cout << "Create One" << std::endl << std::endl;
    this->_resourceFonts.insert(this->_resourceFonts.end(), new    ResourceFont()); //Crashes sometimes here
    this->_resourceFonts.at(this->_resourceFonts.size()-   1)->loadFromFile(fontFile,fSize);

    if (this->_resourceFonts.at(this->_resourceFonts.size()-1)->_font)
    {
        return this->_resourceFonts.at(this->_resourceFonts.size()-   1)->_font;
    }
    else
    {
        this->_resourceFonts.at(this->_resourceFonts.size()-   1)->destroy();
        return NULL;
    }
}

有人知道如何解决这个问题吗?

谢谢你:)

最佳答案

不知道这是否是真正的错误,但代码太大,无法放在评论中。

AddFont 的最后你有:

if (this->_resourceFonts.at(this->_resourceFonts.size()-1)->_font)
{
    return this->_resourceFonts.at(this->_resourceFonts.size()-   1)->_font;
}
else
{
    this->_resourceFonts.at(this->_resourceFonts.size()-   1)->destroy();
    return NULL;
}

此代码测试非空 _font,这意味着在 else 部分中 _font 成员 空。它仍然留在 _resourceFonts vector 中。

关于vector<Object *> use 和 new Object() 的 C++ 编程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35160404/

相关文章:

c++ - 将元素分配给数组不起作用(使用 OpenMP 的并行合并排序)

c++ - 一类嵌套类不能作为模板函数typename

c - 定义 `int *p={1,2,3};` 的确切含义是什么?

c++ - SFINAE 检查 std::less 是否有效

c++ - 为什么内联在这里不起作用?

javascript - 纯JS |篮子 |关闭

c++ - C++ 中的矩阵类 : An Explanation

c - 将字节指针分配给 C 中的结构

python - 如何用 python 方法返回一个新的类对象?

design-patterns - OOP模式设计(数据访问)的常用方法是什么