简体   繁体   English

std::map 中的结构<int,struct> memory 漏电?</int,struct>

[英]Structs in std::map<int,struct> memory leaking?

I have the following struct and map我有以下结构和 map

struct dataStruct{
  unsigned long time;
  int32_t ch0;
  int32_t ch1;
  uint8_t state;
  int16_t temp;
  uint16_t vbat;
  int8_t rssi;
  };

std::map<uint32_t,struct dataStruct> uuidData = {};

And a loop which waits for new data and fills the map with it.还有一个等待新数据并用它填充 map 的循环。 (1) (1)

for(;;)
{
    if (data_debug.is_new_data_available())
    {
        uint32_t uuid = data_debug.get_ID();
        uuidData[uuid] = {
            millis(), 
            data_debug.get_data1(), 
            data_debug.get_data2(), 
            data_debug.get_state(), 
            data_debug.get_temperature(),
            data_debug.get_battery_level(),
            data_debug.get_RSSI()
        };
    }
}

This works, but when I get data with an existing UUID, my intuition is that the old struct never gets deleted and will eventually leak.这行得通,但是当我获取具有现有 UUID 的数据时,我的直觉是旧结构永远不会被删除并且最终会泄漏。

I had an alternative code (2) below that tried to bypass this, but when the UUID was duplicated, the struct was filled with garbage.我在下面有一个替代代码 (2) 试图绕过它,但是当 UUID 被复制时,结构中充满了垃圾。 Is the code in (1) leaking or c++ automatically frees the space when the struct is no longer needed? (1) 中的代码是否泄漏或 c++ 在不再需要结构时自动释放空间? (I know this sounds like garbage collection, but i have no idea how c++ handle structs) (我知道这听起来像是垃圾回收,但我不知道 c++ 如何处理结构)

(2) (2)

    if(uuidData.count(uuid)){
        uuidData[uuid].time = millis();
        uuidData[uuid].ch0 = data_debug.get_data1();
        uuidData[uuid].ch1 = data_debug.get_data2();
        uuidData[uuid].state = data_debug.get_state();
        uuidData[uuid].temp = data_debug.get_temperature();
        uuidData[uuid].vbat = data_debug.get_battery_level();
        uuidData[uuid].time = data_debug.get_RSSI();
    }
    else{
        uuidData[uuid] = {
            millis(), 
            data_debug.get_data1(), 
            data_debug.get_data2(), 
            data_debug.get_state(), 
            data_debug.get_temperature(),
            data_debug.get_battery_level(),
            data_debug.get_RSSI()
            };
}

Let's break down what happens with example 1 (the correct way to do this task )让我们分解示例 1 中发生的情况(执行此任务的正确方法)

uuidData[uuid] = {
            millis(), 
            data_debug.get_data1(), 
            data_debug.get_data2(), 
            data_debug.get_state(), 
            data_debug.get_temperature(),
            data_debug.get_battery_level(),
            data_debug.get_RSSI()
        };

The

        {
            millis(), 
            data_debug.get_data1(), 
            data_debug.get_data2(), 
            data_debug.get_state(), 
            data_debug.get_temperature(),
            data_debug.get_battery_level(),
            data_debug.get_RSSI()
        };

creates and initializes a temporary dataStruct variable, an automatic variable that will only exist until it's no longer needed.创建并初始化一个临时的dataStruct变量,一个自动变量,只有在不再需要它时才会存在。 When the expression ends, the temporary goes out of scope and is destroyed.当表达式结束时,临时变量从 scope 中退出并被销毁。

uuidData[uuid]

looks in uuidData for a key that matches uuid .uuidData中查找与uuid匹配的键。 If it does not find one, it creates an empty dataStruct and maps it to the key.如果找不到,它会创建一个空的dataStruct并将其映射到键。 This new dataStruct is managed by uuidData .这个新的dataStructuuidData管理。 Where it comes from and where it goes is not your concern.它从哪里来,到哪里去,与你无关。 Once a dataStruct mapped to uuid exists, a reference to it is returned.一旦映射到uuiddataStruct存在,就会返回对它的引用。

Now we have a reference to an existing dataStruct in the map and the temporary dataStruct .现在我们在 map 和临时dataStruct中有一个现有的dataStruct的引用。 The = simply copies the contents of the temporary on the right into the the object represented by the reference on the left. =只是简单地将右侧临时的内容复制到左侧引用所代表的 object 中。 Whatever was in the object on the left is overwritten but the object is still there and still managed by uuidData .左侧 object 中的任何内容都被覆盖,但 object 仍然存在并且仍由uuidData管理。 It will be properly destroyed and deallocated when it is either removed from uuidData or uuidData goes out of scope. The temporary is destroyed automatically ( ergo the name Automatic variable ) for you when the expression is complete.当它从uuidData中删除或uuidData超出 scope 时,它将被正确销毁和释放。当表达式完成时,临时变量会自动销毁( 因此名称为自动变量)。

There is no possibility for a leak here.这里没有泄漏的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM