From 6b0ab9459547ca9ae768bae6b9454401d8aeb0ce Mon Sep 17 00:00:00 2001 From: heck Date: Wed, 9 Mar 2022 15:43:41 +0100 Subject: [PATCH] prototype: pEp::String - move alloc/free routines into class --- test/test_nr1.cc | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/test/test_nr1.cc b/test/test_nr1.cc index 21514fa..2972e29 100644 --- a/test/test_nr1.cc +++ b/test/test_nr1.cc @@ -213,7 +213,7 @@ namespace pEp { ~String() { if (_is_owner) { - _free(); + _free(*_c_str_pp); } } @@ -226,7 +226,7 @@ namespace pEp { } // DEALLOCATION - _free(); + _free(*_c_str_pp); // ALLOCATION *_c_str_pp = _copy(str); @@ -296,22 +296,17 @@ namespace pEp { static bool log_enabled; private: - // TODO: this is dodgy, do we really need _c_str_pp AND _c_str_p??? - void _free() + static char* _copy(const std::string& str) { - pEpLogClass("called"); - // if the c_str points to a different address now, than the one we created - if (*_c_str_pp != _c_str_p) { - // a new string has been created, and we need to free it as well - pEpLogClass("raw access string change detected"); - pEp::free(*_c_str_pp); - } else { - // WE ASSUME THAT: - // if the char* has been replaced, it has been freed, as well - if (_c_str_p != nullptr) { - pEp::free(_c_str_p); - } - } + char* ret = strdup(str.c_str()); + pEpLog(CXX::Inspect::all(ret)); + return ret; + } + + static void _free(char* ptr_type) + { + pEpLog(CXX::Inspect::all(ptr_type)); + ::pEp_free(ptr_type); } bool _is_bound{ false };