You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ios::binary读取派生类二进制文件触发虚函数访问违规的解决方法

解决含虚函数类对象二进制序列化后跨进程读取的虚函数调用崩溃问题

问题根源

直接用ios::binary模式读写带虚函数的类对象时,会把对象内存中的虚函数表指针(__vfptr) 一并写入文件。虚表地址是进程私有且程序启动时动态分配的,重启进程后,新进程的虚表地址和之前写入文件的完全不同,读取后对象的__vfptr指向无效内存,调用虚函数时必然触发访问违规。

解决方法

1. 只序列化业务数据,而非整个对象内存

放弃直接读写对象的整个内存块,改为单独写入/读取对象的业务成员变量(比如员工ID、姓名、业绩、底薪等非虚表指针的成员)。读取时先创建新的对象实例,再将读取的数据赋值给对象成员。
示例代码:

// 写入ProductEmployee对象的业务数据
void writeProductEmployee(std::ofstream& out, const ProductEmployee& emp) {
    out.write(reinterpret_cast<const char*>(&emp.id), sizeof(emp.id));
    // 写入字符串时带上结束符,方便读取
    out.write(emp.name.c_str(), emp.name.size() + 1);
    out.write(reinterpret_cast<const char*>(&emp.sales), sizeof(emp.sales));
    out.write(reinterpret_cast<const char*>(&emp.baseSalary), sizeof(emp.baseSalary));
}

// 读取并构造ProductEmployee对象
ProductEmployee readProductEmployee(std::ifstream& in) {
    ProductEmployee emp;
    in.read(reinterpret_cast<char*>(&emp.id), sizeof(emp.id));
    
    char nameBuf[256];
    in.read(nameBuf, sizeof(nameBuf));
    emp.name = nameBuf;
    
    in.read(reinterpret_cast<char*>(&emp.sales), sizeof(emp.sales));
    in.read(reinterpret_cast<char*>(&emp.baseSalary), sizeof(emp.baseSalary));
    
    return emp;
}

2. 多态场景下,给基类添加序列化接口

如果需要支持多态(比如基类指针指向不同派生类对象),可以在基类中定义纯虚的序列化/反序列化接口,让每个派生类实现自己的读写逻辑。同时写入文件时先存一个类型标识,读取时根据标识创建对应类型的对象,再调用反序列化方法。
示例代码:

class Employee {
public:
    virtual double getSalary() const = 0;
    // 纯虚序列化/反序列化接口
    virtual void serialize(std::ofstream& out) const = 0;
    virtual void deserialize(std::ifstream& in) = 0;
    virtual ~Employee() = default;
};

class ProductEmployee : public Employee {
private:
    int id;
    std::string name;
    double sales;
    double baseSalary;
public:
    double getSalary() const override {
        return baseSalary + sales * 0.1;
    }

    void serialize(std::ofstream& out) const override {
        // 先写入类型标识,方便读取时判断对象类型
        int type = 1;
        out.write(reinterpret_cast<const char*>(&type), sizeof(type));
        
        out.write(reinterpret_cast<const char*>(&id), sizeof(id));
        out.write(name.c_str(), name.size() + 1);
        out.write(reinterpret_cast<const char*>(&sales), sizeof(sales));
        out.write(reinterpret_cast<const char*>(&baseSalary), sizeof(baseSalary));
    }

    void deserialize(std::ifstream& in) override {
        in.read(reinterpret_cast<char*>(&id), sizeof(id));
        
        char nameBuf[256];
        in.read(nameBuf, sizeof(nameBuf));
        name = nameBuf;
        
        in.read(reinterpret_cast<char*>(&sales), sizeof(sales));
        in.read(reinterpret_cast<char*>(&baseSalary), sizeof(baseSalary));
    }
};

// 读取任意Employee派生类对象的函数
std::unique_ptr<Employee> readEmployee(std::ifstream& in) {
    int type;
    in.read(reinterpret_cast<char*>(&type), sizeof(type));
    
    std::unique_ptr<Employee> emp;
    if (type == 1) {
        emp = std::make_unique<ProductEmployee>();
        emp->deserialize(in);
    }
    // 扩展其他派生类的类型判断...
    
    return emp;
}

3. 禁止直接内存拷贝的误区

不要尝试通过编译选项去除虚表指针——虚表是C++多态的核心,无法简单禁用,强行操作只会破坏多态功能,完全不可行。

核心思路

问题本质是直接内存拷贝包含了进程相关的虚表指针,解决关键是只序列化对象的业务数据,而非整个内存块,多态场景则通过基类接口实现类型安全的读写逻辑。

内容的提问来源于stack exchange,提问作者user135789

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 18:05:32