如何正确使用和释放asn1cSEQUENCE_OF?
How to correctly use and free asn1c SEQUENCE_OF?
我在很多项目中都使用 asn1c 库,但我从未找到如何使用 SEQUENCE_OF
的免费成员。因此,我总是将它设置为 nullptr
,当我使用 Valgrind 时,我看到(当然)在包含列表的元素上使用 ASN_STRUCT_FREE
时我的列表成员没有被释放。
所以我的问题是如何使用该免费会员?
这是一个简单的例子,说明我如何将我的列表与 asn1c 一起使用。
ListItem_t *li = nullptr;
StructWList_t swl;
swl.list.count = 0;
swl.list.size = 0;
swl.list.free = nullptr; // How can I feed it properly?
swl.list.array = reinterpret_cast<ListItem_t**>(calloc(1, sizeof *swl.list.array));
for(int i = 0 ; i < 5 ; i++)
{
li = reinterpret_cast<ListItem_t*>(calloc(1, sizeof *li));
*li = i;
// Valgrind says that the calloc below is definitly lost
swl.list.array[i] = reinterpret_cast<ListItem_t*>(calloc(1, sizeof *swl.list.array[i]));
ASN_SEQUENCE_ADD(&swl, li);
}
...
ASN_STRUCT_FREE(ASN_DEF_StructWList, &swl);
有谁知道如何正确喂养它?
编辑
我的 asn1c 版本是来自 AUR(在 Archlinux 上)git 存储库的 v0.9.29。
上述ASN.1如下:
Example
DEFINITIONS AUTOMATIC TAGS ::=
BEGIN
StructWList ::= SEQUENCE OF ListItem
ListItem ::= INTEGER
END
提前致谢,
埃米利安
// Valgrind says that the calloc below is definitly lost
swl.list.array[i] = reinterpret_cast<ListItem_t*>(calloc(1, sizeof *swl.list.array[i]));
ASN_SEQUENCE_ADD(&swl, li);
ASN_SEQUENCE_ADD
将覆盖您存储在上一行的指针。您应该像在第一行那样手动存储它,或者调用 ASN_SEQUENCE_ADD
但不能同时调用两者。
您还应该完全初始化 swl
,因为它包含更多成员 (_asn_ctx
),并使用 ASN_STRUCT_FREE_CONTENTS_ONLY
,因为 swl
分配在堆栈上,无法释放。
--- main.cpp.orig 2019-05-07 20:49:25.880336931 +0300
+++ main.cpp 2019-05-07 20:59:10.192431926 +0300
@@ -3,7 +3,7 @@
int main()
{
ListItem_t *li = nullptr;
- StructWList_t swl;
+ StructWList_t swl = {0};
swl.list.count = 0;
swl.list.size = 0;
@@ -15,8 +15,8 @@
li = reinterpret_cast<ListItem_t*>(calloc(1, sizeof *li));
*li = i;
// Valgrind says that the calloc below is definitly lost
- swl.list.array[i] = reinterpret_cast<ListItem_t*>(calloc(1, sizeof *swl.list.array[i]));
+ //swl.list.array[i] = reinterpret_cast<ListItem_t*>(calloc(1, sizeof *swl.list.array[i]));
ASN_SEQUENCE_ADD(&swl, li);
}
- ASN_STRUCT_FREE(ASN_DEF_StructWList, &swl);
+ ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_StructWList, &swl);
}
用g++ -Wall -I. -ggdb -O0 -o test main.cpp libasncodec.a
编译
valgrind --tool=memcheck ./test
==29555== Memcheck, a memory error detector
==29555== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==29555== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==29555== Command: ./test
==29555==
==29555==
==29555== HEAP SUMMARY:
==29555== in use at exit: 0 bytes in 0 blocks
==29555== total heap usage: 9 allocs, 9 frees, 72,848 bytes allocated
==29555==
==29555== All heap blocks were freed -- no leaks are possible
==29555==
==29555== For counts of detected and suppressed errors, rerun with: -v
==29555== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
我在很多项目中都使用 asn1c 库,但我从未找到如何使用 SEQUENCE_OF
的免费成员。因此,我总是将它设置为 nullptr
,当我使用 Valgrind 时,我看到(当然)在包含列表的元素上使用 ASN_STRUCT_FREE
时我的列表成员没有被释放。
所以我的问题是如何使用该免费会员?
这是一个简单的例子,说明我如何将我的列表与 asn1c 一起使用。
ListItem_t *li = nullptr;
StructWList_t swl;
swl.list.count = 0;
swl.list.size = 0;
swl.list.free = nullptr; // How can I feed it properly?
swl.list.array = reinterpret_cast<ListItem_t**>(calloc(1, sizeof *swl.list.array));
for(int i = 0 ; i < 5 ; i++)
{
li = reinterpret_cast<ListItem_t*>(calloc(1, sizeof *li));
*li = i;
// Valgrind says that the calloc below is definitly lost
swl.list.array[i] = reinterpret_cast<ListItem_t*>(calloc(1, sizeof *swl.list.array[i]));
ASN_SEQUENCE_ADD(&swl, li);
}
...
ASN_STRUCT_FREE(ASN_DEF_StructWList, &swl);
有谁知道如何正确喂养它?
编辑
我的 asn1c 版本是来自 AUR(在 Archlinux 上)git 存储库的 v0.9.29。
上述ASN.1如下:
Example
DEFINITIONS AUTOMATIC TAGS ::=
BEGIN
StructWList ::= SEQUENCE OF ListItem
ListItem ::= INTEGER
END
提前致谢,
埃米利安
// Valgrind says that the calloc below is definitly lost swl.list.array[i] = reinterpret_cast<ListItem_t*>(calloc(1, sizeof *swl.list.array[i])); ASN_SEQUENCE_ADD(&swl, li);
ASN_SEQUENCE_ADD
将覆盖您存储在上一行的指针。您应该像在第一行那样手动存储它,或者调用 ASN_SEQUENCE_ADD
但不能同时调用两者。
您还应该完全初始化 swl
,因为它包含更多成员 (_asn_ctx
),并使用 ASN_STRUCT_FREE_CONTENTS_ONLY
,因为 swl
分配在堆栈上,无法释放。
--- main.cpp.orig 2019-05-07 20:49:25.880336931 +0300
+++ main.cpp 2019-05-07 20:59:10.192431926 +0300
@@ -3,7 +3,7 @@
int main()
{
ListItem_t *li = nullptr;
- StructWList_t swl;
+ StructWList_t swl = {0};
swl.list.count = 0;
swl.list.size = 0;
@@ -15,8 +15,8 @@
li = reinterpret_cast<ListItem_t*>(calloc(1, sizeof *li));
*li = i;
// Valgrind says that the calloc below is definitly lost
- swl.list.array[i] = reinterpret_cast<ListItem_t*>(calloc(1, sizeof *swl.list.array[i]));
+ //swl.list.array[i] = reinterpret_cast<ListItem_t*>(calloc(1, sizeof *swl.list.array[i]));
ASN_SEQUENCE_ADD(&swl, li);
}
- ASN_STRUCT_FREE(ASN_DEF_StructWList, &swl);
+ ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_StructWList, &swl);
}
用g++ -Wall -I. -ggdb -O0 -o test main.cpp libasncodec.a
valgrind --tool=memcheck ./test
==29555== Memcheck, a memory error detector
==29555== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==29555== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==29555== Command: ./test
==29555==
==29555==
==29555== HEAP SUMMARY:
==29555== in use at exit: 0 bytes in 0 blocks
==29555== total heap usage: 9 allocs, 9 frees, 72,848 bytes allocated
==29555==
==29555== All heap blocks were freed -- no leaks are possible
==29555==
==29555== For counts of detected and suppressed errors, rerun with: -v
==29555== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)