提高二进制序列化不能偶尔工作。解析的数据有时会损坏 [英] Boost binary serialization doesn't work occasionally. The parsed data is corrupted sometimes

查看:154
本文介绍了提高二进制序列化不能偶尔工作。解析的数据有时会损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原因不明,提振二进制序列不偶尔工作。解析的数据有时会损坏。

本来我手动序列化自定义的类的实例,每个实例是在一个文本文件中的行。然而,速度是缓慢的。(该文本文件处理2 MB /秒的速度,它可以在Windows任务管理器中看到。)最近,我改为使用升压二进制序列化。然而,奇怪的事情发生了。

我存储类InstanceIdentity,其中包含一个字符串成员变量和STRUC成员变量重新presenting的X-Y-Z整数很多很多实例坐标。

我的问题:previous 504实例可以从成功的二进制文件进行解析,并打印到屏幕上。然而,第505实例解析错误(在X-Y-Z坐标应不超过512),然后将下面的情况变得一团糟。很快一个未知的异常是从提升抛出。我试图抓住它,并使用了。什么()方法来显示更多的信息。然而,它只是说未知异常

 行503:029_4_.s_raw.gz(306,215,64)
线504:029_4_.s_raw.gz(224,154,86)
行505:029_4_.s_raw.gz(68109,36608,16384)
//然后屏幕输出变得一团糟。我甚至不能在这里复制的一塌糊涂。

我的试验来解决这个问题。


  1. 我查了一下这个网页的http://boost-users.boost.narkive.com/70Yjldp7/boost-serialization-throws-random-exceptions 。而且我确定和ifstream的ofstream的打开二进制:

    out_file_stream.open(FILE_NAME,的ios_bas​​e ::出来|的ios_bas​​e ::二进制);

    in_file_stream.open(FILE_NAME,的ios_bas​​e ::在|的ios_bas​​e ::二进制);


但问题仍然存在。

<醇开始=2>
  • 我试着使用升压序列化的文本文件中的数据相同。 (仅文件打开方式和提高存档类型需要进行更改。)

  • 中的数据可以被序列化,并通过文本文件成功反序列化。由于这可能意味着我自定义的类InstanceIdentity序列化功能的正确性,升压二进制序列化的问题变得怪异。

    <醇开始=3>
  • 我在开十六进制编辑器升压二进制文件(记事本+ +插件),并试图找到在第505次实例的东西。
    第504实例的XYZ坐标的十六进制是:

    //第一个实例x-Y-Z

    169 = 0xA9 144 = 0×90 = 79 0x4F

    //而且我可以找到00 00 00 A9 00 00 00 90 00 00 00 4F在适当的位置。

    // 502th实例x-Y-Z

    137 = 0x89上268 = 0x10C 136 =均为0x88

    //但是我找不到00 00 00 89 00 00 01 0C 00 00 00在整个二进制文件88,即使这种情况下是正确地解析提振

    // 503th实例x-Y-Z

    306 = 0x132 215 = 0xD7 64 = 0X40

    //但是我找不到00 00 01 32 00 00 00在整个二进制文件D7 00 00 00 40,即使这种情况下是正确地解析提振

    //第504实例x-Y-Z

    224 = 154取0xE0 = 0x9A执行86 = 0x56

    00 00 00 E0 00 00 00 9A 00 00 00 56

    //但是我找不到00 00 00 E0 00 00 00 9A 00 00 00 56在整个二进制文件,即使这个实例正确地解析提振。


  • 问题:
    有谁知道什么升压二进制文件里面发生了什么?
    我应该怎么做我的序列化实例的二进制文件,并从它反序列化呢?
    我使用的是同一台电脑,所以我觉得这个问题是不是便携性。

    环境:
    Windows 8的64位时,Visual Studio 2013,提振1_59_0版本。


    解决方案

    我张贴这个问题非常非常抱歉。升压二进制序列化按预期工作。我只是做了一个小错误,但没有发现它,直到几分钟前。

    我大概是世界上最愚蠢的程序员。在我的功能,我使用了布尔参数 identity_file_in_binary 来让用户选择一个二进制文件是否为preferred。然后,我使用了的if-else 子句运行。开()在不同的模式。我一直在使用 iso_base ::二进制如果范围,但误把它放在其他范围。我纠正这个错误之后,我的程序工作的顺利进行。

    PS:我花了2天发现我的问题,在这期间我已经检查了错误的的if-else 条款很多很多次,但忽视了错误的来源。这件事情是怎么发生的?有没有人有一些好的想法,以prevent这种类型的错误?

    With unknown reason, boost binary serialization doesn't work occasionally. The parsed data is corrupted sometimes.

    Originally I serialized instances of self-defined class manually, with each instance being a line in a text file. However, the speed was slow.(The text file was dealt in a speed of 2 MB/sec, which can be seen in windows task manager.) Recently I changed to use the boost binary serialization. However, strange things happens.

    I stored many many instances of class InstanceIdentity, which contains a string member variable and a struc member variable representing the x-y-z integer coordinates.

    My Problem: The previous 504 instances can be parsed from the binary file successfully and printed to screen. However the 505th instance is falsely parsed.(The x-y-z coordinates should be no more than 512.) And then the following instances become a mess. And soon an unknown exception is thrown from boost. I tried to catch it and used the .what() method to reveal more information. However, it only says Unknown Exception.

    line 503: 029_4_.s_raw.gz (306, 215, 64)
    line 504: 029_4_.s_raw.gz (224, 154, 86)
    line 505: 029_4_.s_raw.gz (68109, 36608, 16384)
    // Then the screen output becomes a mess. I can't even copy the mess here.
    

    My trials to solve it.

    1. I checked this page http://boost-users.boost.narkive.com/70Yjldp7/boost-serialization-throws-random-exceptions . And I made sure the ifstream and ofstream is opened as binary:

      out_file_stream.open(file_name, ios_base::out | ios_base::binary);

      in_file_stream.open(file_name, ios_base::in | ios_base::binary);

    But the problem continues.

    1. I tried to serialize the same data in text file using boost. (Only the file open mode and boost archive type need to be changed.)

    The data can be serialized and deserialized successfully through text file. Since this probably implies the correctness of the serialize function in my self-defined class InstanceIdentity, the problem of boost binary serialization gets weirder.

    1. I opened the boost binary file in hex editor (notepad++ plugin) and tried to find something at the 505th instances. The hex of the xyz coordinates of 504th instance is:

      // 1st instance x-y-z

      169=0xA9 144=0x90 79=0x4F

      // And I can find 00 00 00 a9 00 00 00 90 00 00 00 4f in the appropriate location.

      // 502th instance x-y-z

      137=0x89 268=0x10C 136=0x88

      // But I can't find 00 00 00 89 00 00 01 0c 00 00 00 88 in the whole binary file, even if this instance is correctly parsed by boost.

      // 503th instance x-y-z

      306=0x132 215=0xD7 64=0x40

      // But I can't find 00 00 01 32 00 00 00 d7 00 00 00 40 in the whole binary file, even if this instance is correctly parsed by boost.

      // 504th instance x-y-z

      224=0xE0 154=0x9A 86=0x56

      00 00 00 e0 00 00 00 9a 00 00 00 56

      // But I can't find 00 00 00 e0 00 00 00 9a 00 00 00 56 in the whole binary file, even if this instance is correctly parsed by boost.

    Questions: Does anybody know what's happening inside the boost binary file? What should I do to serialize my instances to binary file and deserialize them from it? I am using the same computer, so I think this problem is not about portability.

    Environment: Windows 8 64 bit, visual studio 2013, boost 1_59_0 version.

    解决方案

    I am very very sorry for posting this question. The boost binary serialization works as expected. I just made a minor mistake but didn't find it until few minutes ago.

    I am probably the stupidest programmer in the world. In my function, I uses a bool parameter identity_file_in_binary to let the user choose whether a binary file is preferred. Then I uses a if-else clause to run .open() in different mode. I should have been using iso_base::binary in the if scope but mistakenly put it in the else scope. After I corrected this mistake, my program worked smoothly.

    PS: I have spent 2 days on finding the source of my problem, during which I have checked the erroneous if-else clause many many times but overlooked the mistake. How could this thing happen? Does anyone have some good ideas to prevent this type of mistakes?

    这篇关于提高二进制序列化不能偶尔工作。解析的数据有时会损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆