Skip to content

char* members in value types marshalled as string result in a memory leak #1785

Closed
@Saalvage

Description

@Saalvage

The following type

CS_VALUE_TYPE class DLL_API ValueType {
public:
    const char* char_ptr_member;
};

results in a memory leak when the following code is executed:

while (true) {
    var test = new ValueType();
    test.CharPtrMember = "Looooooooooooong string";
}

The set accessor allocates memory which is never freed outside of the set accessor.

At this time I see three options to solve this, listed in presumed increasing level of complexity:

  1. Disabling char* to string marshalling for value type members
  2. Using IDisposable
  3. Wrapping the unmanaged memory in a managed class that cleans up the memory when it gets finalized and having that class be an additional member of the value type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions