Skip to content

Have an ACE_CString (or equiv) take ownership of a char* (from an ACE_Read_Buffer or equiv) #2308

Open
@alexchandel

Description

@alexchandel

Version

Any / Latest (8.0.2)

Host machine and operating system

Any / Windows 11

Target machine and operating system (if different from host)

Any

Compiler name and version (including patch level)

Any

The $ACE_ROOT/ace/config.h file

Any (win32)

The $ACE_ROOT/include/makeinclude/platform_macros.GNU file

if you use a link to a platform-specific file, simply state which one (unless this isn't used in this case, e.g., with Microsoft Visual C++)

AREA/CLASS/EXAMPLE AFFECTED:

String_Base / ACE_CString

The problem effects:

Semantics.

Synopsis

ACE_CString cannot take ownership of a char * allocated by a ACE_Allocator.

Description

ACE_CString can copy a char* into a new allocation with an ACE_Allocator (which it will free), or it can use an existing pointer but will not free it. Noteably, there is no way to take ownership of a char* (allocated by an ACE_Allocator) and free it when necessary.

This is a problem, because ACE_Read_Buffer->read returns just such a bare pointer. This encourages the bad practice of direction pointer management (as the only examples with ACE_Read_Buffer do), rather than the safe C++ practice of RAII.

Repeat by

ACE_Read_Buffer buffer(fp, true);
char* fileContents = buffer.read(-1, 0, 0);

// exception is thrown, memory leak

buffer.alloc()->free(fileContents);

Sample fix/ workaround

Not aware of any correct workaround. One can technically often use a CORBA::String_var, but this only works by coincidence, as its destructor always calls delete[], which is what the default ACE_Allocator does (ACE_New_Allocator), but this is not generally correct code. Is there any ACE RAII class that can take a pointer (and a pointer to the ACE_Allocator that allocated it) and take ownership of it, and free it in its destructor? Is there a higher-level buffered file-reading class ACE_Read_Buffer that returns an RAII-managed pointer?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions