Smart pointer and thread programming

Discussion in 'C++' started by Jack, Aug 5, 2007.

  1. Jack

    Jack Guest

    Hi,

    In multi-thread programming, if I using smart pointers to store
    pointers to the same dynamically allocated objects, can memory leak be
    avoided?

    For example, two threads share the same dynamically allocated objects
    using smart pointer. The reference to the shared object is 2. If one
    thread crashes before decreasing the reference, then eventually the
    reference will still be 1, so the object will not be deleted. So this
    is memory leak.
    Am I right?

    Thanks.

    Jack
     
    Jack, Aug 5, 2007
    #1
    1. Advertising

  2. * Jack:
    > Hi,
    >
    > In multi-thread programming, if I using smart pointers to store
    > pointers to the same dynamically allocated objects, can memory leak be
    > avoided?
    >
    > For example, two threads share the same dynamically allocated objects
    > using smart pointer. The reference to the shared object is 2. If one
    > thread crashes before decreasing the reference, then eventually the
    > reference will still be 1, so the object will not be deleted. So this
    > is memory leak.
    > Am I right?


    "Crash" of a thread is not a very meaningful concept.

    But let's say you mean one of the threads terminate without executing
    destructors.

    Then yes you have a problem. The solution might be to execute
    destructors. Or it might be to terminate the whole process.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Aug 5, 2007
    #2
    1. Advertising

  3. Jack

    Ian Collins Guest

    Jack wrote:
    > Hi,
    >
    > In multi-thread programming, if I using smart pointers to store
    > pointers to the same dynamically allocated objects, can memory leak be
    > avoided?
    >
    > For example, two threads share the same dynamically allocated objects
    > using smart pointer. The reference to the shared object is 2. If one
    > thread crashes before decreasing the reference, then eventually the
    > reference will still be 1, so the object will not be deleted. So this
    > is memory leak.


    Typically the only way for a thread to "crash" without terminating the
    application is for it to be canceled.

    There isn't a standard answer to this question, because the C++ standard
    has nothing to say on the subject of threads. How the destruction of a
    smart pointer is handled is down to the design of the object and the
    runtime library.

    --
    Ian Collins.
     
    Ian Collins, Aug 5, 2007
    #3
  4. Jack wrote:
    > Hi,
    >
    > In multi-thread programming, if I using smart pointers to store
    > pointers to the same dynamically allocated objects, can memory leak be
    > avoided?
    >
    > For example, two threads share the same dynamically allocated objects
    > using smart pointer. The reference to the shared object is 2. If one
    > thread crashes before decreasing the reference, then eventually the
    > reference will still be 1, so the object will not be deleted. So this
    > is memory leak.
    > Am I right?


    It is the responsibility of the application to maintain a consistent
    state. If your thread "crashes" and leaves your application state in a
    poor state then you are better off terminating the entire process.
     
    Gianni Mariani, Aug 5, 2007
    #4
  5. "Jack" <> wrote in message
    news:...
    > Hi,
    >
    > In multi-thread programming, if I using smart pointers to store
    > pointers to the same dynamically allocated objects, can memory leak be
    > avoided?
    >
    > For example, two threads share the same dynamically allocated objects
    > using smart pointer. The reference to the shared object is 2. If one
    > thread crashes before decreasing the reference, then eventually the
    > reference will still be 1, so the object will not be deleted. So this
    > is memory leak.
    > Am I right?


    Your correct. However, your threads should not get into the habit of just
    crashing in random places... Humm, that would be a real nasty habit...

    ;^0
     
    Chris Thomasson, Aug 5, 2007
    #5
  6. Jack

    Guest

    On Aug 5, 12:51 pm, Jack <> wrote:
    > Hi,
    >
    > In multi-thread programming, if I using smart pointers to store
    > pointers to the same dynamically allocated objects, can memory leak be
    > avoided?
    >
    > For example, two threads share the same dynamically allocated objects
    > using smart pointer. The reference to the shared object is 2. If one
    > thread crashes before decreasing the reference, then eventually the
    > reference will still be 1, so the object will not be deleted. So this
    > is memory leak.
    > Am I right?
    >
    > Thanks.
    >
    > Jack


    Possible alternatives:

    1. See if you can set your code to check reference count of the object
    just before your main exits.

    2. See if some exception could be thrown when your thread "crashes".
    But in a good program a thread shouldnot crash unpredictably, so check
    for bugs.
     
    , Aug 6, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vyacheslav Kononenko
    Replies:
    0
    Views:
    4,105
    Vyacheslav Kononenko
    May 13, 2005
  2. Protoman
    Replies:
    28
    Views:
    745
    mlimber
    Aug 14, 2006
  3. coala
    Replies:
    3
    Views:
    399
    coala
    Sep 6, 2006
  4. coala
    Replies:
    1
    Views:
    608
    Victor Bazarov
    Sep 6, 2006
  5. Hicham Mouline
    Replies:
    100
    Views:
    2,139
    Noah Roberts
    Aug 25, 2009
Loading...

Share This Page