Smart pointer and thread programming

J

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?

Thanks.

Jack
 
A

Alf P. Steinbach

* 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.
 
I

Ian Collins

Jack said:
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.
 
G

Gianni Mariani

Jack said:
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.
 
C

Chris Thomasson

Jack said:
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
 
A

amardeep.developer

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top