Why lifetime of temporary doesn't extend till lifetime of enclosing object?
Tag : cpp , By : John Phipps
Date : March 29 2020, 07:55 AM
Does that help The standard considers two circumstances under which the lifetime of a temporary is extended: B* f() {
B * bp = new B(A());
return b;
}
void test() {
B* p = f();
delete p;
}
|
Track QStandardItem lifetime
Date : March 29 2020, 07:55 AM
I wish this help you As is often the case in Qt, there are objects that are not QObjects, but that are managed by a QObject (or otherwise accessible via one). You need to make MyObject monitor the model the items are in. The code below could be a starting point. Another approach, not implemented but certainly feasible, is to dynamically replace all items in a model with copies that are instances that you yourself created. By monitoring the relevant model signals, you can be notified of all item additions and replace items with instances that you are a factory for. It would be a thinly veiled dependency injection into a QStandardItemModel. class MyObject : public QObject {
Q_OBJECT
QStandardItem * m_item;
Q_SLOT void onRowsAboutToBeRemoved(const QModelIndex & parent, int start, int end) {
if (m_item->parent() == parent &&
m_item->index().row() >= start &&
m_item->index().row() <= end) onItemGone;
}
Q_SLOT void onColumnsAboutToBeRemoved(const QModelIndex & parent, int start, int end) {
if (m_item->parent() == parent &&
m_item->index().column() >= start &&
m_item->index().column() <= end) onItemGone;
}
Q_SLOT void onItemGone() {
m_item = 0;
deleteLater();
}
public:
MyObject(QStandardItem* item, QObject * parent = 0) :
QObject(parent), m_item(item)
{
Q_ASSERT(m_item.model());
connect(m_item.model(), SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
SLOT(onRowsAboutToBeRemoved(QModelIndex,int,int)));
connect(m_item.model(), SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
SLOT(onColumnsAboutToBeRemoved(QModelIndex,int,int)));
connect(m_item.model(), SIGNAL(modelAboutToBeReset()), SLOT(onItemGone());
connect(m_item.model(), SIGNAL(destroyed()), SLOT(onItemGone());
}
};
|
Cannot infer an appropriate lifetime for lifetime parameter cloning trait object
Tag : rust , By : Pradeep Gowda
Date : March 29 2020, 07:55 AM
seems to work fine The error message is quite confusing in this case, but notice a double ampersand here: (expected &&std::collections::HashMap >, found &&std::collections::HashMap>). It looks like it tries to clone the reference. I assume you wanted to clone the entire HashMap. Calling clone explicitly as Clone::clone(spiders) gives much clearer error message:error[E0277]: the trait bound `Spider: std::clone::Clone` is not satisfied
error[E0277]: the trait bound `Spider: std::marker::Sized` is not satisfied
--> error_orig.rs:19:26
|
19 | let thread_spiders = Clone::clone(spiders);
| ^^^^^^^^^^^^ the trait `std::marker::Sized` is not implemented for `Spider`
|
= note: `Spider` does not have a constant size known at compile-time
= note: required because of the requirements on the impl of `std::clone::Clone` for `Box<Spider>`
= note: required because of the requirements on the impl of `std::clone::Clone` for `std::collections::HashMap<std::string::String, Box<Spider>>`
= note: required by `std::clone::Clone::clone`
pub trait Spider: Sync + Send {
fn add_request_headers(&self, headers: &mut Vec<String>);
fn clone_into_box(&self) -> Box<Spider>;
}
impl Clone for Box<Spider> {
fn clone(&self) -> Self {
self.clone_into_box()
}
}
#[derive(Clone)]
pub struct Google {}
impl Spider for Google {
fn add_request_headers(&self, headers: &mut Vec<String>) {
headers.push("Hello".to_string())
}
fn clone_into_box(&self) -> Box<Spider> {
Box::new(self.clone())
}
}
|
Using Mutex to track another app's lifetime
Tag : chash , By : Michael Gunderson
Date : March 29 2020, 07:55 AM
wish helps you This pseudocode enables MyApp1 to wait for MyApp2 to launch and then detect when user exits MyApp2: HANDLE hMutex = nullptr;
while (hMutex == nullptr && !timedout)
{
hMutex = OpenMutex(
SYNCHRONIZE,
FALSE,
"MyApp2IsRunning");
Sleep(1000);
}
if (timedout){return error;}
DWORD wait_result = WaitForSingleObject(hMutex, INFINITE);
|
Lifetime of object in lambda connected to pyqtSignal
Date : March 29 2020, 07:55 AM
With these it helps The lambda in your example forms a closure. That is, it is a nested function that references objects available in its enclosing scope. Every function that creates a closure keeps a cell object for every item it needs to maintain a reference to. In your example, the lambda creates a closure with references to the local self and model variables inside the scope of the __init__ method. If you keep a reference to the lambda somewhere, you can examine all the cell objects of its closure via its __closure__ attribute. In your example, it would display something like this: >>> print(func.__closure__)
(<cell at 0x7f99c16c5138: MyModel object at 0x7f99bbbf0948>, <cell at 0x7f99c16c5168: MyClass object at 0x7f99bbb81390>)
TypeError: 'managedbuffer' object is not callable
|