By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,510 Members | 1,810 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,510 IT Pros & Developers. It's quick & easy.

Smart pointer and thread programming

P: n/a
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

Aug 5 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
Aug 5 '07 #2

P: n/a
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.
Aug 5 '07 #3

P: n/a
"Jack" <ju******@gmail.comwrote in message
news:11**********************@z24g2000prh.googlegr oups.com...
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

Aug 5 '07 #4

P: n/a
On Aug 5, 12:51 pm, Jack <junw2...@gmail.comwrote:
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 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.