protobuf 使用中发现的一个问题
2013-05-16 18:08
429 查看
首先先说一下版本
protoc --version
2.5.0
然后说一下bug是什么,就是在把一个对象序列化成一个ostringstream后,如果再把另一个对象序列化到同一个ostringstream后,会发现第二个对象变成了两个对象。
即使ostringstream.clear() 后一样会有问题
先上协议文件, 标准的example
package lm;
message helloworld
{
required int32 id = 1; // ID
required string str = 2; // str
optional int32 opt = 3; //optional field
repeated int32 value = 4;
}
然后再上测试代码,
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include "helloworld.pb.h"
using namespace std;
using namespace lm;
using namespace google::protobuf;
int main()
{
helloworld hello;
hello.set_id( 100 );
hello.set_str( "this is a test ");
hello.set_opt( 200 );
for( int i=0; i<100; ++i)
{
hello.add_value( i );
}
ostringstream ss;
hello.SerializeToOstream( &ss );
string data = ss.str();
cout << data.length() << endl;;
cout << data << endl;
ss.flush(); //even flush is not work
ss.clear(); // attention:we have clear the stream
helloworld hello2;
hello2.set_id( 100 );
hello2.set_str( "this is a test ");
hello2.set_opt( 200 );
for( int i=0; i<100; ++i)
{
hello2.add_value( i );
}
hello2.SerializeToOstream( &ss );
string data2 = ss.str();
cout << data2 <<endl;
cout << data2.length() << endl;
return 0;
}
结果是
222
dthis is a test �
! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c
dthis is a test �
! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b dthis is a test �
! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c
444
显然,两个同样的hello对象连在一起了.
我一开始觉得这应该是protobuf的一个bug, 因为按照正常的stream使用标准,在我们clear,flush后,不应该在流中保留已有的东西。
为了验证我的想法,我马上又写了一个小test, 来排除是stringstream的问题。
代码如下,
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ostringstream ss;
ss.str("haha");
string data = ss.str();
cout << data.length() << endl;
ss.clear();
data = ss.str();
cout << data.length() << endl;
return 0;
}
我以为输出应该是4,0,但是很意外结果是4,4
这说明了,不是protobuf的问题,而是stringstream的问题,在clear,flush之后,并没有清空流。
最后上google搜了一下,为什么stringstream.clear 不起作用。
发现有大把人跟我遇到过一样的问题啊
在clear之前,加一句 ss.str( string() );
即设置一个空的string,通过这种方式来清空流。即,需要同时清空流,以及流中的string
做到这里先告一段落,后续我需要看看stringstream的原码,看看为什么不置空string就达不到清空流的目的!
protoc --version
2.5.0
然后说一下bug是什么,就是在把一个对象序列化成一个ostringstream后,如果再把另一个对象序列化到同一个ostringstream后,会发现第二个对象变成了两个对象。
即使ostringstream.clear() 后一样会有问题
先上协议文件, 标准的example
package lm;
message helloworld
{
required int32 id = 1; // ID
required string str = 2; // str
optional int32 opt = 3; //optional field
repeated int32 value = 4;
}
然后再上测试代码,
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include "helloworld.pb.h"
using namespace std;
using namespace lm;
using namespace google::protobuf;
int main()
{
helloworld hello;
hello.set_id( 100 );
hello.set_str( "this is a test ");
hello.set_opt( 200 );
for( int i=0; i<100; ++i)
{
hello.add_value( i );
}
ostringstream ss;
hello.SerializeToOstream( &ss );
string data = ss.str();
cout << data.length() << endl;;
cout << data << endl;
ss.flush(); //even flush is not work
ss.clear(); // attention:we have clear the stream
helloworld hello2;
hello2.set_id( 100 );
hello2.set_str( "this is a test ");
hello2.set_opt( 200 );
for( int i=0; i<100; ++i)
{
hello2.add_value( i );
}
hello2.SerializeToOstream( &ss );
string data2 = ss.str();
cout << data2 <<endl;
cout << data2.length() << endl;
return 0;
}
结果是
222
dthis is a test �
! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c
dthis is a test �
! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b dthis is a test �
! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c
444
显然,两个同样的hello对象连在一起了.
我一开始觉得这应该是protobuf的一个bug, 因为按照正常的stream使用标准,在我们clear,flush后,不应该在流中保留已有的东西。
为了验证我的想法,我马上又写了一个小test, 来排除是stringstream的问题。
代码如下,
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ostringstream ss;
ss.str("haha");
string data = ss.str();
cout << data.length() << endl;
ss.clear();
data = ss.str();
cout << data.length() << endl;
return 0;
}
我以为输出应该是4,0,但是很意外结果是4,4
这说明了,不是protobuf的问题,而是stringstream的问题,在clear,flush之后,并没有清空流。
最后上google搜了一下,为什么stringstream.clear 不起作用。
发现有大把人跟我遇到过一样的问题啊
在clear之前,加一句 ss.str( string() );
即设置一个空的string,通过这种方式来清空流。即,需要同时清空流,以及流中的string
做到这里先告一段落,后续我需要看看stringstream的原码,看看为什么不置空string就达不到清空流的目的!
相关文章推荐
- mac 关于使用protobuf出现ld: symbol(s) not found for architecture x86_64的问题
- iOS开发中使用protobuf遇到的一些问题
- 使用netty+zookeeper+protobuf实现一个RPC过程
- 发现Java StringBuffer使用中的一个隐藏问题,你中招了吗?
- ios开发使用xcode6打包protobuf静态库 解决了 官方引用 protobuf 导致的冲突问题 包含 arm64 支持
- c++ 使用pbc 解决lua 解析protobuf 问题
- protobuf2.5在iOS上编译及cocos2dx混合使用的问题
- Protobuf使用不当导致的程序内存上涨问题
- 关于使用protobuf出现undefined reference 的问题
- Protobuf使用不当导致的程序内存上涨问题
- 发现Java StringBuffer使用中的一个隐藏问题,你中招了吗?
- protobuf进行ParseFromString解包失败的一个常见问题
- JFinal极速开发框架使用笔记(二) 两个问题,一个发现
- 使用匿名方法产生的一个难发现问题
- protobuf 使用enum时的问题
- 在eclipse中使用protobuf-dt插件进行序列化时遇到的问题
- 最近在使用sps类库过程中发现了一个让我比较疑惑的问题(有关items属性的)
- HTML DOM insertBefore() 方法 使用的时候发现一个问题,记录下
- 发现使用TabContainer的一个小问题
- 在使用JdbcTemplate.queryForMap(String )的时候发现一个问题.