Title: Leaked memory in `IfcPropertySingleValue.NominalValue` with a newly allocated `IfcValue` · Issue #8070 · IfcOpenShell/IfcOpenShell · GitHub
Open Graph Title: Leaked memory in `IfcPropertySingleValue.NominalValue` with a newly allocated `IfcValue` · Issue #8070 · IfcOpenShell/IfcOpenShell
X Title: Leaked memory in `IfcPropertySingleValue.NominalValue` with a newly allocated `IfcValue` · Issue #8070 · IfcOpenShell/IfcOpenShell
Description: Bug Description setNominalValue() stores the new pointer in the property through set_attribute_value() 1 2, but the new value is not clearly owned by IfcFile. Later, when IfcFile is destroyed, that new value is not deleted by the normal ...
Open Graph Description: Bug Description setNominalValue() stores the new pointer in the property through set_attribute_value() 1 2, but the new value is not clearly owned by IfcFile. Later, when IfcFile is destroyed, that...
X Description: Bug Description setNominalValue() stores the new pointer in the property through set_attribute_value() 1 2, but the new value is not clearly owned by IfcFile. Later, when IfcFile is destroyed, that...
Opengraph URL: https://github.com/IfcOpenShell/IfcOpenShell/issues/8070
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Leaked memory in `IfcPropertySingleValue.NominalValue` with a newly allocated `IfcValue`","articleBody":"### Bug Description\n\n`setNominalValue()` stores the new pointer in the property through `set_attribute_value()` [1](#ref1) [2](#ref2), but the new value is not clearly owned by `IfcFile`. Later, when `IfcFile` is destroyed, that new value is not deleted by the normal file cleanup path [5](#ref5).\n\nAs I understand it, this affects non-entity IFC values such as `IfcLabel`, `IfcReal`, `IfcBoolean`, `IfcInteger`, etc.\n\nExample:\n\n```cpp\n#include \u003cifcparse/IfcFile.h>\n#include \u003cifcparse/Ifc4.h>\n\n#include \u003cstdexcept>\n\nint main()\n{\n IfcParse::IfcFile file(\"/path/to/any/ifc-file/that/has/any/property/in/the/file.ifc\");\n\n Ifc4::IfcPropertySingleValue::list::ptr properties =\n file.instances_by_type\u003cIfc4::IfcPropertySingleValue>();\n\n Ifc4::IfcPropertySingleValue* prop = nullptr;\n Ifc4::IfcValue* oldValue = nullptr;\n // Find any existing property.\n for (Ifc4::IfcPropertySingleValue::list::it it = properties->begin();\n it != properties->end();\n ++it)\n {\n oldValue = (*it)->NominalValue(); // (A) existing value loaded from file\n if (oldValue != nullptr)\n {\n prop = *it;\n break;\n }\n }\n\n if (prop == nullptr)\n throw std::runtime_error(\"No IfcPropertySingleValue with NominalValue found\");\n\n prop->setNominalValue(new Ifc4::IfcLabel(\"new\")); // (B) replacement value\n\n return 0;\n}\n```\n\nWhat i expect:\n\n- `IfcLabel(\"new\")` should be owned and deleted by the file\n- or the API should make it clear that the caller still owns `IfcLabel(\"new\")` and must delete it.\n\nActual behavior:\n\n- `IfcLabel(\"new\")` is stored in the property attribute [2](#ref2).\n- It is not added to the file's owned entity map by `setNominalValue()` [1](#ref1) [2](#ref2).\n- It is not deleted by `IfcFile::~IfcFile()` [5](#ref5).\n\n### Detail\n\nAfter `setNominalValue()`, there are two relevant resources:\n\n- (A) the old `NominalValue`, originally loaded from the IFC file\n- (B) the new `NominalValue`, created by caller code with `new Ifc4::IfcLabel(\"new\")`\n\nFor (A), the old value:\n\n- The value came from the IFC file parser.\n- In current IfcOpenShell, simple type instances read from the file are stored as `std::unique_ptr\u003cIfcUtil::IfcBaseClass>` in `read_simple_type_instances`.\n- Those `unique_ptr`s live in the file storage, so the file has an ownership path for file-loaded simple type instances [6](#ref6).\n\n=> So (A) is fine, not leaked\n\nFor (B), the new replacement value:\n\n- The caller allocates it with `new Ifc4::IfcLabel(\"new\")`.\n- It is freshly allocated.\n- It was not read by the file parser.\n- It is not stored in `read_simple_type_instances`.\n- It is not an IFC entity with a STEP id.\n- It is created after the file was loaded.\n\nThen `setNominalValue()` is called:\n\n```cpp\nprop->setNominalValue(new Ifc4::IfcLabel(\"new\"));\n```\n\nThe generated setter only forwards the raw pointer to `set_attribute_value()`:\n\n```cpp\nvoid Ifc4::IfcPropertySingleValue::setNominalValue(::Ifc4::IfcValue* v) {\n set_attribute_value(2, v->as\u003cIfcUtil::IfcBaseClass>());\n if constexpr (false) unset_attribute_value(2);\n}\n```\n\n`set_attribute_value()` then stores the raw pointer in the property's attribute storage:\n\n```cpp\nif constexpr (std::is_pointer_v\u003cT>) {\n if (t) {\n data_.set_attribute_value(storage, &declaration(), id() ? id() : identity(), i, t);\n } else {\n data_.set_attribute_value(storage, &declaration(), id() ? id() : identity(), i, Blank{});\n }\n}\n```\n\nThis is where the replacement pointer is stored in the attribute slot [2](#ref2).\n\nIt also updates inverse indexes:\n\n- old inverse unregister [2](#ref2)\n- new inverse register [2](#ref2)\n\nBut this is only relationship bookkeeping. It does not make the file own (B).\n\nSpecifically, `set_attribute_value()` does not call `IfcFile::addEntity()` for (B) [2](#ref2) [3](#ref3), and it does not put (B) into `read_simple_type_instances` [6](#ref6).\n\nHowever, there is a non-entity path in `IfcFile::addEntity()` that can put non-entity instances into `tbyid_`:\n\n```cpp\n} else if (new_entity->file_ == nullptr) {\n new_entity->file_ = this;\n\n std::visit([new_entity](auto& m) {\n if constexpr (std::is_same_v\u003cstd::decay_t\u003cdecltype(m)>, impl::in_memory_file_storage>) {\n m.tbyid_.insert({ new_entity->identity(), new_entity });\n }\n }, storage_);\n}\n```\n\nThis path stores non-entity instances in `tbyid_` [3](#ref3) [4](#ref4).\n\nHowever, (B) never goes through `addEntity()` when assigned through `setNominalValue()` [1](#ref1) [2](#ref2).\n\nEven if caller code manually called `file.addEntity(newValue)`, the current destructor still only deletes `byid_` entries:\n\n```cpp\nIfcParse::IfcFile::~IfcFile() {\n for (const auto& p : byid_) {\n delete p.second;\n }\n}\n```\n\nThe destructor only iterates `byid_` [5](#ref5).\n\nSo `tbyid_` is not an obvious safe ownership/deletion path either.\n\n**With all of above**, that means (B) is left in this state:\n\n- the property attribute points to it\n- inverse indexes may know about the relationship\n- the file does not clearly own it\n- `IfcFile::~IfcFile()` does not clearly delete it\n\n=> (B) appears to leak.\n\nI havent check the python API yet. but it likely we have the same problem.\n\n### References\n\n1. \u003ca id=\"ref1\">\u003c/a>Generated `IfcPropertySingleValue::setNominalValue()` only forwards to `set_attribute_value()`: https://github.com/IfcOpenShell/IfcOpenShell/blob/v0.8.0/src/ifcparse/Ifc4.cpp#L12258\n2. \u003ca id=\"ref2\">\u003c/a>`IfcUtil::IfcBaseClass::set_attribute_value()` unregisters old inverse references, stores the new pointer, and registers new inverse references: https://github.com/IfcOpenShell/IfcOpenShell/blob/v0.8.0/src/ifcparse/IfcParse.cpp#L1129\n3. \u003ca id=\"ref3\">\u003c/a>`IfcFile::addEntity()` non-entity path sets `file_` and inserts into `tbyid_`: https://github.com/IfcOpenShell/IfcOpenShell/blob/v0.8.0/src/ifcparse/IfcParse.cpp#L2118\n4. \u003ca id=\"ref4\">\u003c/a>`in_memory_file_storage` contains `byid_` and `tbyid_` maps: https://github.com/IfcOpenShell/IfcOpenShell/blob/v0.8.0/src/ifcparse/storage.h#L257.\n5. \u003ca id=\"ref5\">\u003c/a>`IfcFile::~IfcFile()` deletes entries from `byid_` only: https://github.com/IfcOpenShell/IfcOpenShell/blob/v0.8.0/src/ifcparse/IfcFile.cpp#L574\n6. \u003ca id=\"ref6\">\u003c/a>File-loaded simple type instances are stored as `read_simple_type_instances` unique pointers and moved into file storage after parsing:\n - https://github.com/IfcOpenShell/IfcOpenShell/blob/v0.8.0/src/ifcparse/storage.h#L191\n - https://github.com/IfcOpenShell/IfcOpenShell/blob/v0.8.0/src/ifcparse/IfcParse.cpp#L566\n - https://github.com/IfcOpenShell/IfcOpenShell/blob/v0.8.0/src/ifcparse/IfcParse.cpp#L1608\n\n\n### Attachments\n\n_No response_\n\n### Debug and Error Output\n\n```true\n\n```","author":{"url":"https://github.com/philong6297","@type":"Person","name":"philong6297"},"datePublished":"2026-05-17T08:38:25Z","url":"https://github.com/IfcOpenShell/IfcOpenShell/issues/8070"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:33405141-7dc3-9582-20b9-3aaf8b71835e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AE6A:26A2E8:6D8547:92F4E0:6B04AA72 |
| html-safe-nonce | d69fa67589c40ad150b3f91a052f329638a85833ca61f566eb8a78cf145b2693 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRTZBOjI2QTJFODo2RDg1NDc6OTJGNEUwOjZCMDRBQTcyIiwidmlzaXRvcl9pZCI6Ijg4NDI5MTAzNjY1NTMxMjM0NDIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2356986d1f582379063bd763540672e13dc39a2e973283243993c58dc8d1b30f |
| hovercard-subject-tag | issue:4462875583 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/IfcOpenShell/IfcOpenShell/8070/issue_layout |
| twitter:image | https://opengraph.githubassets.com/02069df7fda060123a0f8357b88827b8f98aceb2cabb46204fecbe94f9a90651/IfcOpenShell/IfcOpenShell/issues/8070 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/02069df7fda060123a0f8357b88827b8f98aceb2cabb46204fecbe94f9a90651/IfcOpenShell/IfcOpenShell/issues/8070 |
| og:image:alt | Bug Description setNominalValue() stores the new pointer in the property through set_attribute_value() 1 2, but the new value is not clearly owned by IfcFile. Later, when IfcFile is destroyed, that... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | philong6297 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 188497a0f45a7aa8f7c866faa1950d1a7f8d82b59416adf549b222ad1d1dd19b |
| turbo-cache-control | no-preview |
| go-import | github.com/IfcOpenShell/IfcOpenShell git https://github.com/IfcOpenShell/IfcOpenShell.git |
| octolytics-dimension-user_id | 13692563 |
| octolytics-dimension-user_login | IfcOpenShell |
| octolytics-dimension-repository_id | 40473201 |
| octolytics-dimension-repository_nwo | IfcOpenShell/IfcOpenShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 40473201 |
| octolytics-dimension-repository_network_root_nwo | IfcOpenShell/IfcOpenShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 557cfbac6cdb532730c95c90f2cefdc0c4128029 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width