简体   繁体   English

撤消/重做时释放内存时出现问题

[英]Problems when freeing memory when I have undo/redo

I'm working on a project for a class where we are supposed to implement basic functionality for a text editor, including undo and redo. 我正在为一个类的项目工作,该类应该为文本编辑器实现基本功能,包括撤消和重做。 I currently have my undo/redo functions working correctly, the only problem is, is that I'm getting a valgrind error when trying to free the memory from a Command object that tells us what to execute. 目前,我的撤消/重做功能正常运行,唯一的问题是,当尝试从Command对象中释放内存以告知执行什么时,我遇到了valgrind错误。

This Command object is part of a struct within a namespace like so: Command对象是命名空间中结构的一部分,如下所示:

struct UserInteraction
{
    UserInteractionType type;
    Command* command;
};

Currently, I store each command that the user issues in a std::vector of type <UserInteraction> called undoStack . 当前,我将用户发出的每个命令存储在名为undoStack的类型为<UserInteraction>的std :: vector中。 I also have a redoStack of the same type which is also a std::vector. 我也有相同类型的redoStack ,它也是std :: vector。 Storing each command looks like this: 存储每个命令如下所示:

    else if (interaction.type == UserInteractionType::command) // COMMAND
    {
        try
        {
            interaction.command->execute(editor);
            commandInteraction = true;
            undoStack.push_back(interaction);
            view.clearErrorMessage();
        }
        catch (EditorException& e)
        {
            delete interaction.command;
            view.showErrorMessage(e.getReason());
        }
        view.refresh();
    } // end if-else

I need to deallocate each UserInteraction that was pushed into the vector, when the user presses ctrl+x (which exits the program). 当用户按下ctrl + x(退出程序)时,我需要释放分配到向量中的每个UserInteraction。 Otherwise, any command I issue gives me a error like this: 否则,我发出的任何命令都会给我这样的错误:

4 bytes in 1 blocks are definitely lost in loss record 2 of 38

Where each command entered increases the amount of bytes by lost by 4. 输入的每个命令将丢失的字节数增加4。

I figured I just needed to deallocate all the memory used by each vector within the quit command, but all the current solutions that people use seem to still give me memory leaks. 我认为我只需要在quit命令中重新分配每个向量使用的所有内存,但是人们目前使用的所有当前解决方案似乎仍然给我带来内存泄漏。 Here are two versions of my quit method (both of which still give me memory leaks): 这是我的退出方法的两个版本(这两个版本仍然会给我带来内存泄漏):

First try: 第一次尝试:

    if (interaction.type == UserInteractionType::quit)
    {
        clear undo
        std::vector<UserInteraction>().swap(undoStack);
        clear redo
        std::vector<UserInteraction>().swap(redoStack);
        break;
    }

And my second try at deallocating memory: 我第二次尝试分配内存:

    if (interaction.type == UserInteractionType::quit)
    {
        undoStack.clear();
        undoStack.shrink_to_fit();
        redoStack.clear();
        redoStack.shrink_to_fit();
        break;
    }

So how can I actually deallocate everything in both vectors? 那么,我如何才能实际释放这两个向量中的所有内容?

By freeing the memory of your vector, you'll only destroy your UserInteractions, but not the commands they are pointing to. 通过释放引导程序的内存,您只会破坏UserInteractions,而不会破坏它们指向的命令。 If you use a c++11 compiler you can use a smart pointer. 如果使用c ++ 11编译器,则可以使用智能指针。

struct UserInteraction
{
    UserInteractionType type;
    std::unique_ptr<Command> command;
    // or
    std::shared_ptr<Command> command;
};

That will automatically take care of deleting the commands. 这将自动处理删除命令。 If not, I`d just delete all the commands in a loop iterating through the vector elements. 如果没有,我将在循环遍历矢量元素的循环中删除所有命令。 (You could also write a costum destructor that deletes the command, together with a copy-assignment operator and -ctor (deep copy), as well as move-assignment operator and -ctor, but that seems like an overly complex solution) (您还可以编写一个costum析构函数来删除命令,同时使用复制分配运算符和-ctor(深层副本)以及移动分配运算符和-ctor来删除该命令,但这似乎是一个过于复杂的解决方案)

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

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