Be sure of the creation of an object



  • Hello,

    Is there a way to be sure of the creation of an object ?
    for example, if I write :

    QLabel *lbl = new QLabel("Hello world");
    

    How can I be sure that the label is created ? Is there any signal , or something ?



  • @NGV1

    Simply test the pointer. It should be zero when the object is not created.


  • Moderators

    @koahnig said:

    @NGV1

    Simply test the pointer. It should be zero when the object is not created.

    You would probably get an exception too: http://stackoverflow.com/questions/6833143/how-to-check-memory-allocation-failures-with-new-operator

    @NGV1 said:

    How can I be sure that the label is created ? Is there any signal , or something ?

    What will you do if the label fails to be created?



  • Thanks for answering,
    @JKSH
    If it fail, either I display an error or I try it again,


  • Moderators

    You're welcome :)

    @NGV1 said:

    If it fail, either I display an error or I try it again,

    I have never seen it fail on my PCs.

    Anyway, the only reason I can think of for failure is when you use up all your RAM. How will you display an error when you don't have any RAM left?

    Also, if it fails the first time, why would it succeed when you try again?



  • In fact, it's just to know if it's creating or if it's created, and after a timeout, I consider that it fail, so it's not necessarily a RAM issue.
    Moreover, while I didn't create this object, I don't try to create the following.

    Thanks for the link, I'll study it !


  • Lifetime Qt Champion

    Hi,

    I must say that's is pretty strange concept, how are you implementing it ? What is your use case ?


  • Moderators

    @NGV1 said:

    In fact, it's just to know if it's creating or if it's created, and after a timeout, I consider that it fail

    Um... new QLabel("Hello world"); cannot possibly timeout. It will either instantly succeed or instantly fail.

    You cannot check if an object is "in the middle of creation".

    Where did you learn these ideas from?



  • If this is not a completely hypothetical discussion and you see some sort of a timeout, it sounds more like a daggling pointer. When you allocate memory as you do in the little example and it goes out of focus, the pointer will be overwritten eventually on the stack.



  • Thanks every body,
    I'm developing, a Bluetooth application. The bluetooth trames reception should manage the display, and I would like to be sure that nothing is forgotten. So when I receive a Bluetooth trame (after a request), I send an acknowledgement with checksum, and I send an other request when I finish the treatment of this trame. That's why I would like to be sure that the objects are created, to avoid error because of the modification of an object that does not exist.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.