string 析构崩溃 assign

  • 崩溃
(gdb) bt
#0  0x00007f5a4eb24418 in _int_free () from /lib64/libc.so.6
#1  0x00007f5a4f440c03 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() () from /lib64/libstdc++.so.6
#2  0x000000000042ad93 in CEndpoint::FileMD5 (this=0x7f59d80010c0, event=..., scan_list=std::list = {...}, info=...) at ../Endpoint.cpp:322

...

#7  0x00007f5a50eeae25 in start_thread () from /lib64/libpthread.so.0
#8  0x00007f5a4eba034d in clone () from /lib64/libc.so.6
(gdb) f 2
#2  0x000000000042ad93 in CEndpoint::FileMD5 (this=0x7f59d80010c0, event=..., scan_list=std::list = {...}, info=...) at ../Endpoint.cpp:322
(gdb) p uploadJSON
$1 = "{\"FilePath\":\"c:\\\\windows\\\\ey87mb9scd\\\\x86\\\\secopatcher.dll\",\"MD5\":\"e0f0683bb8cfd4413eccd777034e6a20\"}"
  • 代码

析构中报错

      /**
       *  @brief  Destroy the string instance.
       */
      ~basic_string() _GLIBCXX_NOEXCEPT
      { _M_rep()->_M_dispose(this->get_allocator()); }

_M_dispose

	void
	_M_dispose(const _Alloc& __a)
	{
#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
	  if (__builtin_expect(this != &_S_empty_rep(), false))
#endif
	    {
	      // Be race-detector-friendly.  For more info see bits/c++config.
	      _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount);
	      if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
							 -1) <= 0)
		{
		  _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount);
		  _M_destroy(__a);
		}
	    }
	}  // XXX MT

_M_destroy 在basic_string.tcc定义

  template<typename _CharT, typename _Traits, typename _Alloc>
    void
    basic_string<_CharT, _Traits, _Alloc>::_Rep::
    _M_destroy(const _Alloc& __a) throw ()
    {
      const size_type __size = sizeof(_Rep_base) +
	                       (this->_M_capacity + 1) * sizeof(_CharT);
      _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
    }

_Alloc deallocate 释放内存

  /**
   * @brief  Uniform interface to all allocator types.
   * @ingroup allocators
  */
  template<typename _Alloc>
    struct allocator_traits
    {
      /// The allocator type
      typedef _Alloc allocator_type;
      /// The allocated type
      typedef typename _Alloc::value_type value_type;
 
     /**
       *  @brief  Deallocate memory.
       *  @param  __a  An allocator.
       *  @param  __p  Pointer to the memory to deallocate.
       *  @param  __n  The number of objects space was allocated for.
       *
       *  Calls <tt> a.deallocate(p, n) </tt>
      */
      static void deallocate(_Alloc& __a, pointer __p, size_type __n)
      { __a.deallocate(__p, __n); }

 
      ...
    
    }

代码中是用assign来 str.assign(str2),str2 是通过rapidjson GetString()获得,    basic_string.h头文件内容中声明basic_string& assign(const basic_string& __str);

    #include <ext/atomicity.h>
    #include <debug/debug.h>
    #if __cplusplus >= 201103L
        #include <initializer_list>
    #endif

     /**
       *  @brief  Set value to contents of another string.
       *  @param  __str  Source string to use.
       *  @return  Reference to this string.
       */
      basic_string&
      assign(const basic_string& __str);

basic_string.h 中 #include <debug/debug.h>, 在debug.h中找到assign定义

    basic_string&
    assign(const basic_string& __x)
    {
      _Base::assign(__x);
      this->_M_invalidate_all();
      return *this;
    }

str析构的时候崩溃

如果在当前脚本中定义了一个 public static 字典,并在该脚本中赋值,但在其他脚本中引用该字典却为空,有可能是以下几个原因: 1. 赋值时机不对:如果在其他脚本中引用该字典的时候,当前脚本中的赋值操作还没有执行,那么字典就会为空。可以通过在 Awake 或 Start 方法中赋值来保证赋值时机的正确性。 2. 脚本执行顺序不对:Unity 中每个脚本都有执行的顺序,如果当前脚本的执行顺序在其他脚本之后,那么其他脚本在引用该字典时,当前脚本中的赋值还未执行,导致字典为空。可以通过编辑器中的“Script Execution Order”来调整脚本的执行顺序。 3. 字典被重置了:如果在其他脚本中对该字典进行了赋值或者清空操作,那么原本赋值的字典就会被重置为空。可以通过在当前脚本的 OnDisable 或 OnDestroy 方法中保存字典的状态,或者使用单例模式来保证字典的不被重置。 以下是使用单例模式共享字典对象的示例代码: ```csharp // DictionaryManager.cs using System.Collections.Generic; using UnityEngine; public class DictionaryManager : MonoBehaviour { public static DictionaryManager Instance { get; private set; } public Dictionary<string, string> dict = new Dictionary<string, string>(); private void Awake() { // 实现单例模式 if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); } else { Destroy(gameObject); } // 初始化字典 dict.Add("apple", "red"); dict.Add("banana", "yellow"); dict.Add("orange", "orange"); } } // OtherScript.cs using UnityEngine; public class OtherScript : MonoBehaviour { private void Start() { // 获取字典 var dict = DictionaryManager.Instance.dict; // 输出字典中的内容 foreach (var kvp in dict) { Debug.Log(kvp.Key + ": " + kvp.Value); } } } ``` 在上述代码中,定义了一个 DictionaryManager 类,其中包含一个名为 dict 的字典对象。在 Awake() 方法中,实现了单例模式,并且初始化了字典。在 OtherScript 类中,通过 DictionaryManager.Instance 获取单例对象,并访问其中的 dict 字典对象,最后输出了字典中的内容。这样就可以在多个脚本中共享同一个字典对象了。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HeathXian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值