1
0
Fork 0

Fix memory allocation issues in Json class.

This commit is contained in:
Max Neunhoeffer 2014-07-25 15:09:13 +02:00
parent b6122ad3dc
commit 7c983d0f7b
2 changed files with 25 additions and 18 deletions

View File

@ -219,14 +219,10 @@ void testExecutionPlans () {
cout << "Before assignment" << jj.toString() << endl; cout << "Before assignment" << jj.toString() << endl;
jj = j; // this steals the pointer from j jj = j; // this steals the pointer from j
cout << "Before copy" << jj.toString() << endl;
jj = j.copy(); // this does a copy, but both are now NOFREE
cout << j.get("a").toString() << endl;
cout << jjjj.toString(); cout << jjjj.toString();
cout << jj.toString(); cout << jj.toString();
Json k = j.get("c"); Json k = jj.get("c");
Json l = k.at(2); Json l = k.at(2);
cout << l.toString() << endl; cout << l.toString() << endl;

View File

@ -479,7 +479,7 @@ namespace triagens {
/// copying the whole structure. /// copying the whole structure.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Json (Json const& j) Json (Json& j)
: _zone(j._zone), _json(j.steal()), _autofree(j._autofree) { : _zone(j._zone), _json(j.steal()), _autofree(j._autofree) {
std::cout << "Hallo copy constructor" << std::endl; std::cout << "Hallo copy constructor" << std::endl;
} }
@ -490,7 +490,7 @@ namespace triagens {
/// copying the whole structure. /// copying the whole structure.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Json (Json const&& j) Json (Json&& j)
: _zone(j._zone), _json(j.steal()), _autofree(j._autofree) { : _zone(j._zone), _json(j.steal()), _autofree(j._autofree) {
std::cout << "Hallo, move constructor at work for " std::cout << "Hallo, move constructor at work for "
<< this->toString() << std::endl; << this->toString() << std::endl;
@ -522,16 +522,17 @@ namespace triagens {
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief steal the TRI_json_t*, that is, in the AUTOFREE case the object /// @brief steal the TRI_json_t*, that is, in the AUTOFREE case the pointer
/// is changed into a NOFREE one. This is used in the copy constructor /// _json is changed to a nullptr. This is used in the copy and the move
/// and in the cast operator to TRI_json_t*. /// constructor and in the cast operator to TRI_json_t*.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
TRI_json_t* steal () const throw() { TRI_json_t* steal () throw() {
TRI_json_t* res = _json;
if (_autofree == AUTOFREE) { if (_autofree == AUTOFREE) {
_autofree = NOFREE; _json = nullptr;
} }
return _json; return res;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -548,7 +549,7 @@ namespace triagens {
/// people will use. If you need an actual copy, use the copy method. /// people will use. If you need an actual copy, use the copy method.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Json& operator= (Json const& j) { Json& operator= (Json& j) {
std::cout << "= called" << std::endl; std::cout << "= called" << std::endl;
if (_json != nullptr && _autofree == AUTOFREE) { if (_json != nullptr && _autofree == AUTOFREE) {
TRI_FreeJson(_zone, _json); TRI_FreeJson(_zone, _json);
@ -563,7 +564,7 @@ namespace triagens {
/// @brief move assignment operator, this has steal semantics. /// @brief move assignment operator, this has steal semantics.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Json& operator= (Json const&& j) { Json& operator= (Json&& j) {
std::cout << "= move called" << std::endl; std::cout << "= move called" << std::endl;
if (_json != nullptr && _autofree == AUTOFREE) { if (_json != nullptr && _autofree == AUTOFREE) {
TRI_FreeJson(_zone, _json); TRI_FreeJson(_zone, _json);
@ -582,7 +583,12 @@ namespace triagens {
Json c; Json c;
std::cout << "ATTENTION: recursive JSON copy performed!!!" << std::endl; std::cout << "ATTENTION: recursive JSON copy performed!!!" << std::endl;
c._zone = _zone; c._zone = _zone;
c._json = TRI_CopyJson(_zone, _json); if (_json != nullptr) {
c._json = TRI_CopyJson(_zone, _json);
}
else {
c._json = nullptr;
}
c._autofree = _autofree; c._autofree = _autofree;
return c; return c;
} }
@ -763,7 +769,12 @@ namespace triagens {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::string toString () { std::string toString () {
return JsonHelper::toString(_json); if (_json != nullptr) {
return JsonHelper::toString(_json);
}
else {
return std::string("");
}
} }
private: private:
@ -784,7 +795,7 @@ namespace triagens {
/// @brief flag, whether we automatically free the TRI_json_t*. /// @brief flag, whether we automatically free the TRI_json_t*.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
mutable autofree_e _autofree; autofree_e _autofree;
}; };
} }