Questions tagged [ownership-semantics]

Ownership semantics are a set of rules that govern the lifetime of allocated resources. Ownership semantics determine when and how allocated resources can be freed, and whether ownership can be shared.

54 questions
115
votes
11 answers

Smart pointers: who owns the object?

C++ is all about memory ownership - aka ownership semantics. It is the responsibility of the owner of a chunk of dynamically allocated memory to release that memory. So the question really becomes who owns the memory. In C++ ownership is documented…
Martin York
  • 234,851
  • 74
  • 306
  • 532
49
votes
3 answers

Bad practice to return unique_ptr for raw pointer like ownership semantics?

I've written a static factory method that returns a new Foobar object populated from another data object. I've recently been obsessed with ownership semantics and am wondering if I'm conveying the right message by having this factory method return a…
Bret Kuhns
  • 3,814
  • 5
  • 27
  • 42
20
votes
1 answer

How does Rust move stack variables that are not Copyable?

There is a great example of Rust's move semantics documented here: Rust Move Semantics on the Rust By Example website. I have a basic understanding of both cases demonstrated. The first being how a primitive can have a new alias and the original can…
Ralph Caraveo
  • 9,399
  • 7
  • 36
  • 50
11
votes
2 answers

How to call a method that consumes self on a boxed trait object?

I have the following sketch of an implementation: trait Listener { fn some_action(&mut self); fn commit(self); } struct FooListener {} impl Listener for FooListener { fn some_action(&mut self) { println!("{:?}", "Action!!"); …
WorldSEnder
  • 4,399
  • 1
  • 24
  • 53
11
votes
3 answers

How can I reuse a box that I have moved the value out of?

I have some non-copyable type and a function that consumes and (maybe) produces it: type Foo = Vec; fn quux(_: Foo) -> Option { Some(Vec::new()) } Now consider a type that is somehow conceptually very similar to Box: struct NotBox
Pan Hania
  • 436
  • 4
  • 11
8
votes
1 answer

Generic function accepting &str or moving String without copying

I want to write a generic function that accepts any kind of string (&str/String) for convenience of the caller. The function internally needs a String, so I'd also like to avoid needless re-allocation if the caller calls the function with…
Kornel
  • 91,239
  • 30
  • 200
  • 278
8
votes
5 answers

Accessing owner in destructor c++

Say there is an object A which owns an object B via std::unique_ptr. Further B holds a raw pointer(weak) reference to A. Then the destructor of A will invoke the destructor of B, since it owns it. What will be a safe way to access A in the…
ksb
  • 610
  • 8
  • 18
8
votes
3 answers

Explicitly expressing ownership in Delphi

I'm primarily a C++ programmer, and I've grown used to having class templates like std::unique_ptr, std::shared_ptr, etc for expressing ownership of my objects. Does Delphi have anything that is similar in its standard library? Are there any…
bstamour
  • 7,534
  • 1
  • 21
  • 36
7
votes
3 answers

What does it mean to have sole ownership of object for unique_ptr?

I know that std::unique_ptr is used when an object has only one owner and std::shared_ptr is used when an object has multiple owners. What does it mean to be the unique owner of an object? Does being the unique owner mean that nobody else gets to…
JayZ
  • 194
  • 7
6
votes
1 answer

When is the storage reclaimed for a resource that is no longer owned?

There is a vector resource that is allocated in line 2 of the program below. When the program ends, the vector resource is not owned. If a resource is not owned at all, when does it get reclaimed? Is there an explanation using the terminology of…
Anthony Maida
  • 581
  • 3
  • 8
6
votes
12 answers

How do I *not* delete a member in a destructor?

I'd like the destructor of my class to delete the entire object except for one of the members, which is deleted elsewhere. First of all, is this totally unreasonable? Assuming it's not, how do I do this? I thought that created an destructor with…
Joe
  • 1,091
  • 1
  • 13
  • 18
5
votes
1 answer

Ownership of QAction

When adding a QAction* to a QMenu who is responsible for deleting the QAction* object? I couldn't find it mentioned in the documentation for QMenu or QAction. void MyClass::contextMenuEvent(QContextMenuEvent *evt) { QMenu menu(this); QAction…
glennr
  • 1,756
  • 2
  • 19
  • 33
5
votes
3 answers

Clone String to Specific Lifetime

I'm currently trying to write a little command line app in Rust and I've hit a wall with lifetimes. extern crate clap; use self::clap::{App, Arg}; use std::env; impl<'p> Params<'p> { fn get_username_arg<'r>() -> Arg<'r, 'r> { let mut…
5
votes
2 answers

unique_ptr ownership semantics

Perhaps I was trying to be too generic. (Original question below) Concretely, I have some dependency Dep of a class Foo. I also have a class MockDep and am defining a class TestFoo. Here is its constructor I tried to…
Alec
  • 5,479
  • 3
  • 27
  • 44
5
votes
2 answers

Is there a proper 'ownership-in-a-package' for 'handles' available?

Handles have proper semantics other than pointers. So for me an example like this (extracted from the Rule of Zero): class module { public: explicit module(std::wstring const& name) : handle { ::LoadLibrary(name.c_str()), &::FreeLibrary }…
pepper_chico
  • 9,656
  • 4
  • 46
  • 108
1
2 3 4