Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
alexchandel opened this issue Dec 17, 2024 · 1 comment

Comments

@alexchandel
Copy link

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?

@mitza-oci
Copy link
Member

Try std::unique_ptr<char[]> or std::unique_ptr<char, CustomDeleter>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants