473,804 Members | 2,962 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Smart pointer and thread programming

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
4 1889
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
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
"Jack" <ju******@gmail .comwrote in message
news:11******** **************@ z24g2000prh.goo glegroups.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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1710
by: Sreeram | last post by:
Hello, I am having a doubt about smart pointer. I wrote a smart ptr class with limited functionalities. I want to do like this! CSmartPtr <CSomeClass> test = new CSomeClass; // here i need to call a function which will start a thread and returns once it starts the thread. Here i should able to increment the refcount of smart pointer and should decrement once the thread finishes executing. What i
28
2831
by: Protoman | last post by:
I'd like to now if this smart pointer class if thread-safe: Code: // COPYRIGHT CMDR DOUGLAS I. PEREIRA 07/10/06 // ALL UNAUTHORIZED THIRD PARTY USE IS PROHIBITED // I HAVE WORKED VERY HARD AND HAVE SPENT HOURS OF // TIME DEVELOPING THIS. DO NOT COPY IT OR I WILL SUE YOU // ************************************************************ #pragma once
92
5138
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers, but I just don't know. I don't like them because I don't trust them. I use new and delete on pure pointers instead. Do you use smart pointers?
14
18212
by: Ian | last post by:
I am looking at porting code from a C++ application to C#. This requires implementing data sharing functionality similar to what is provided by a smart pointer in C++. I have only recently begun to work in C# and am asking for suggestions/comments of how to implement a similar data sharing technique in C#. A C++ smart pointer can be used to share common information. For example, assume information managed by objects I1, I2, I3,...
33
5088
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
4
1747
by: Deep | last post by:
I'm in doubt about what is smart pointer. so, please give me simple description about smart pointer and an example of that. I'm just novice in c++. regards, John.
54
12030
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining the advantages of smart pointers endlessly (which are currently used in all our C++ software; we use the Boost smart pointers) as I'm seriously concerned that there is a shift to raw pointers. We are not developing system software but rather...
3
1735
by: mczard | last post by:
Do you know about any other smart pointer libraries? I have found only three: Boost, Loki and Yasper, but I think no one of these is perfect. If I don't find more, I will probably have to write one by myself.
50
4521
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10561
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.