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

returning readonly reference #8

Closed
jedwards1211 opened this issue Sep 29, 2020 · 3 comments
Closed

returning readonly reference #8

jedwards1211 opened this issue Sep 29, 2020 · 3 comments

Comments

@jedwards1211
Copy link
Contributor

I've got this method (want to use an immutable object pattern, not concerned about max performance):

  public static Length Create(double value, LengthUnit unit) {
    Length length = new Length();
    length.Set(value, unit);
    return length;
  }

I think this output C++ code leaks memory? I'm not an expert in C++ but I don't see a way that the shared pointer could be released:

const Length * Length::create(double value, LengthUnit unit)
{
	const Length * length = std::make_shared<Length>().get();
	length->set(value, unit);
	return length;
}
@pfusik
Copy link
Collaborator

pfusik commented Sep 29, 2020

This should be:

public static Length# Create(double value, LengthUnit unit) {
    Length# length = new Length();
    length.Set(value, unit);
    return length;
}

Diagnostics need to be improved - assigning a new object to a non-shared pointer is obviously a leak.

@pfusik pfusik closed this as completed in bdabc48 Sep 29, 2020
@pfusik
Copy link
Collaborator

pfusik commented Sep 29, 2020

Error added - it catches cases such as:

Length p = new Length();
Length Create() { return new Length(); }
void Foo(Length length) { } ... Foo(new Length);

with the message:

ERROR: Dynamically allocated object must be assigned to a Length# reference

@jedwards1211
Copy link
Contributor Author

Thanks! Yeah I understand Length# works, as stated in another issue I'm wanting to use an immutable class pattern and wish I could get a simpler C++ API for it.

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

No branches or pull requests

2 participants