I have a simple question.
I'm writing a program in which a parent process forks and execute multiple children.
I have the impression that when a parent process kills or create a child process the pids of the other children change.
Is that true?
Related
From this post here, in general:
All QObjects will delete their own child objects automatically. (See
docs here.) QWidgets are QObjects. So as long as you establish a
parent/child relationship, you do not need to manually delete your
objects. To do that, simply pass a pointer to the parent object to the
constructor:
QLabel *label1 = new QLabel; // <<- NEED TO DELETE
QLabel *label2 = new QLabel(some_parent_obj); // Will be deleted when some_parent_obj is deleted
So some questions arises:
Does every widget necessary needed/required a parent? If no, what are the exceptions? If yes, what happens to widgets without parent?
I asked this because from examples in Qt Docs, some example widgets have parents (QLabel example) but some doesn't (QBarChart example, and also QFont, QColor, etc...).
So I'm wondering if there's an exception, or those widgets just don't need any parents, or if I declare them with new for some reason, I have to delete afterward.
And vice versa...
Does a widget without parent guarantee to cause a memory leak (or something similar) when the widget which it stays in (not necessary its parent) is deleted? Or if it's removed from a layout without any deletion happening?
Because from my experience with my code, I've created probably quite a lot (~100) of widgets and other stuffs without neither setting any parent (nor using delete afterward), and the project appears to run fine without any stalls even after a while (the effect might be underlying though, as I haven't run Memcheck), hence this question is here.
Does every widget necessary needed/required a parent?
If you want them to be deleted automatically - yes. But...
If no, what are the exceptions? If yes, what happens to widgets without parent?
You do not need to provide a parent to widget if you attach it to layout using QLayout::addWidget. If you look into source code, you'll see that when you do so, it automatically attaches layout's parent as widget's parent(unless you didn't attach layout to any widget).
But if you leave the widget created with new without parent and do not attach to anything - it is leaking memory. You must delete it either using delete or QObject::deleteLater. The last option is recommended when object has any connections.
Does a widget without parent guarantee to cause a memory leak (or something similar) when the widget which it stays in (not necessary its parent) is deleted? Or if it's removed from a layout without any deletion happening?
As I already mention QLayout::addWidget sets parent for added widget, so the answer is no. Also note, that when you call QLayout::removeWidget, it removes only QLayoutItem from layout, but widget's parent stays the same as it was after calling QLayout::addWidget.
When I kill parent job few of the child jobs are still running . how to fix it to kill all child jobs when we kill parent job
Set the job_terminator attribute in the jil of each of the child jobs to TRUE so they will terminate when their containing box terminates.
For example, consider the code below:
Rectangle {
id: idRectParent
Rectangle {
id: idRectChild1
component.onCompleted: {
console.log("Iam Child 1")
}
}
Rectangle {
id: idRectChild2
component.onCompleted: {
console.log("Iam Child 2")
}
}
component.onCompleted : {
console.log("Iam parent Rect")
}
}
I'm getting the output below if I run it in qmlscene (I have tried almost 50 times).
Iam parent Rect
Iam Child 2
Iam Child 1
Why is the output in the above order, instead of:
Iam parent Rect
Iam Child 1
Iam Child 2
or
Iam Child 1
Iam Child 2
Iam parent Rect
or any other combination.
The order is undefined:
Emitted after component "startup" has completed. This can be used to
execute script code at startup, once the full QML environment has been
established.
The corresponding handler is onCompleted. It can be
declared on any object. The order of running the onCompleted handlers
is undefined.
http://qt-project.org/doc/qt-5/qml-qtqml-component.html#completed-signal
The order is undefined but it's meaningful (and can be empirically observed) that when OnCompleted method is called for one item all of its children are instantiated and safely accessible (and the properties initialized), even if OnCompleted event for these children is not called yet.
The order is dictated by C++ classes and for simple declarations the order remains the same from Qt 4.8 through 5.15.
The example describes a QML component containing items. C++ calls occur in this order:
QQmlComponent::beginCreate() is called to create the component.
QQmlObjectCreator::createInstance() is called to create each item in the component.
QQmlObjectCreator::createInstance() is called recursively for each child declared inside an item.
If a child item has no children, it is added to the componentAttached stack for later completion.
After all children are created via their respective QQmlObjectCreator::createInstance() calls, the parent's QQmlObjectCreator::createInstance() continues executing and the parent is added to the componentAttached stack for later completion.
The QQmlComponent::beginCreate() call exits.
QQmlComponent::completeCreate() is called to complete the component.
The componentAttached stack is emptied and onCompleted emitted as each item is popped off the stack.
This enforces the following order:
idRectParent is constructed in QQmlObjectCreator::createInstance() and children begin construction via recursive calls to QQmlObjectCreator::createInstance() before the function exits for idRectParent.
idRectChild1 finishes QQmlObjectCreator::createInstance() and is pushed onto the componentAttached stack.
idRectChild2 finishes QQmlObjectCreator::createInstance() and is pushed onto the componentAttached stack.
idRectParent finishes QQmlObjectCreator::createInstance() and is pushed onto the componentAttached stack.
QQmlComponent::completeCreate() calls QQmlObjectCreator::finalize() and the componentAttached stack is emptied and onCompleted emitted for each item:
idRectParent is popped of the componentAttached stack and emits the onCompleted signal.
idRectChild2 is popped of the componentAttached stack and emits the onCompleted signal.
idRectChild1 is popped of the componentAttached stack and emits the onCompleted signal.
This order is maintained no matter how deep the heiarchy of items: parent first then children in reverse order of declaration.
I have one child say CH_1 of parent application PA_1 and i want to access the properties of that CH_1 from child CH_2 of parent application PA_2 in single flex application.
Hope this question is understandable.
Waiting for reply.
I'm having some troubles with Flex with regards to changing controls on different viewstack panels. At runtime, certain controls aren't accessible and come back as null. Here's an example structure:
viewstack1
canvasPeople
datagridPeople
canvasDetails
tabNavigator1
canvasPersonDetail
txtLastname
canvasPersonOptions
comboboxOccupation
I have a "click" handler in datagrid1 set up to activate canvasB (viewstack1.selectedChild = canvasB) and the detail options box (tabNavigator1.selectedChild = canvasPersonOptions). The former works OK but the latter returns an error about accessing a null object, because at the moment the user clicks on an item in the People datagrid, the tabNavigator1 is null, as is any control underneath it. (its parent, though (canvasDetails), is addressable.) Currently this is a tiny application, so it's not like it's waiting to load the items. Is there a way to control how Flex renders controls so they're accessible before the user sees them? I don't know if this is specific to the viewStack control.
Yes, this is referred to as "creation policy" and it's a property on all Container classes. To do what you we're looking for, you can use a creation policy of "all"; "auto" is the default.
A creationPolicy of ContainerCreationPolicy.AUTO means that the container delays creating some or all descendants until they are needed, a process which is known as deferred instantiation. This policy produces the best startup time because fewer UIComponents are created initially. However, this introduces navigation delays when a user navigates to other parts of the application for the first time. Navigator containers such as Accordion, TabNavigator, and ViewStack implement the ContainerCreationPolicy.AUTO policy by creating all their children immediately, but wait to create the deeper descendants of a child until it becomes the selected child of the navigator container.
A creationPolicy of ContainerCreationPolicy.ALL means that the navigator containers immediately create deeper descendants for each child, rather than waiting until that child is selected. For single-view containers such as a VBox container, there is no difference between the ContainerCreationPolicy.AUTO and ContainerCreationPolicy.ALL policies.
More info here:
http://livedocs.adobe.com/flex/3/html/layoutperformance_04.html
Be careful with this though: Flex picked AUTO as the default for a reason. If your app grows large, the initialization time under creationPolicy=all will be large.
The creation of subcomponents is controlled by the creationPolicy property. It is described in detail in the Flex 3 documentation page "About the creationPolicy property".
If you set the property to "all", every subcomponent of the ViewStack container will be created as soon as the ViewStack is created. The same works for the TabNavigator. Of course this might have some performance implications, which are also described in the documentation.