Click here to Skip to main content
15,905,915 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Smart pointers Pin
Pascal Ganaye15-Nov-11 11:46
Pascal Ganaye15-Nov-11 11:46 
GeneralRe: Smart pointers Pin
JackDingler15-Nov-11 12:01
JackDingler15-Nov-11 12:01 
GeneralRe: Smart pointers Pin
Pascal Ganaye15-Nov-11 21:52
Pascal Ganaye15-Nov-11 21:52 
GeneralRe: Smart pointers Pin
JackDingler16-Nov-11 2:04
JackDingler16-Nov-11 2:04 
GeneralRe: Smart pointers Pin
Erudite_Eric15-Nov-11 22:32
Erudite_Eric15-Nov-11 22:32 
GeneralRe: Smart pointers Pin
Stefan_Lang17-Nov-11 4:37
Stefan_Lang17-Nov-11 4:37 
GeneralRe: Smart pointers Pin
Pascal Ganaye17-Nov-11 4:59
Pascal Ganaye17-Nov-11 4:59 
GeneralRe: Smart pointers Pin
Stefan_Lang17-Nov-11 5:47
Stefan_Lang17-Nov-11 5:47 
I did not want to start a religious war either, but you did mention to use this in a multithreaded environment, and that is a very dangerous place to go! That is why my response may have seemed a bit radical. But my concerns are real enough.

Pascal Ganaye wrote:
They are not in a worst state than calling an object that has been freed.
The . or -> operators can check if the pointer is null or not.

Ouch!

First, your 'owner' has no idea at all what state other objects are in. None! For all you know they may have called a lengthy operation on the object that pointer points to and awaiting a response from somewhere else. Now your 'owner' nullifies all pointers, then the response the aforementioned operation waited for arrives. At this point, the execution continues. However, the object it was called upon has been destroyed, all member variables are undefined, the this pointer is invalid, causing any attempt to call another member function to crash! And you call this 'not in a worst state'?

Second, the . operator can not be overloaded! You can overload -> , but apart from preventing an immediate crash, what should it do? And besides, you cannot even prevent a crash: operator -> does (at least) two things: it dereferences the pointer and then uses the resulting address to access the referenced member. If the nullifiying happens just after the dereferencing, but before accessing the member, this access will crash your application! (in truth, a lot more atomic operations happen upon dereferencing, I just picked the important two to make a point; the important thing to note is that C++ operations can - and will - be interrupted in mid-swing by multi-threading. Even a simple operation such as ++i can be interrupted, between reading i and incrementing the value and writing it back to storage)

Third, even if you could overload both operators, and in a meaningful way, you would have to do that for every single class!

As I said, I'm looking through the multi-threading glasses at this, and what I see isn't pretty. You might be able to fix a few of these concerns, but not all. And some might even apply to single-threaded apps.

Again: do look at existing smart pointers. They're good. They're clever. A horde of extremely clever people created them, used them, improved them, and made them both easy to use and fool-proof. And yes, that includes multi-threading. There's really no reason to roll your own, certainly not when you're using a compiler that already supports C++11 (or at least that part of it).
GeneralRe: Smart pointers Pin
Pascal Ganaye18-Nov-11 0:09
Pascal Ganaye18-Nov-11 0:09 
GeneralRe: Smart pointers Pin
Stefan_Lang18-Nov-11 0:28
Stefan_Lang18-Nov-11 0:28 
QuestionCreateFile after CloseHandle Pin
aangerma14-Nov-11 23:49
aangerma14-Nov-11 23:49 
AnswerRe: CreateFile after CloseHandle Pin
User 742933814-Nov-11 23:58
professionalUser 742933814-Nov-11 23:58 
GeneralRe: CreateFile after CloseHandle Pin
aangerma15-Nov-11 0:09
aangerma15-Nov-11 0:09 
AnswerRe: CreateFile after CloseHandle Pin
User 742933815-Nov-11 0:20
professionalUser 742933815-Nov-11 0:20 
GeneralRe: CreateFile after CloseHandle Pin
aangerma15-Nov-11 0:35
aangerma15-Nov-11 0:35 
GeneralRe: CreateFile after CloseHandle Pin
Erudite_Eric15-Nov-11 1:37
Erudite_Eric15-Nov-11 1:37 
GeneralRe: CreateFile after CloseHandle Pin
Vaclav_15-Nov-11 10:45
Vaclav_15-Nov-11 10:45 
GeneralRe: CreateFile after CloseHandle Pin
Erudite_Eric15-Nov-11 21:19
Erudite_Eric15-Nov-11 21:19 
QuestionHide folder in window explorer Pin
MKC00214-Nov-11 22:40
MKC00214-Nov-11 22:40 
AnswerRe: Hide folder in window explorer Pin
Richard MacCutchan14-Nov-11 23:07
mveRichard MacCutchan14-Nov-11 23:07 
GeneralRe: Hide folder in window explorer Pin
MKC00215-Nov-11 21:33
MKC00215-Nov-11 21:33 
GeneralRe: Hide folder in window explorer Pin
Richard MacCutchan15-Nov-11 21:46
mveRichard MacCutchan15-Nov-11 21:46 
Questionconvert double to WCHAR Pin
jkirkerx14-Nov-11 16:32
professionaljkirkerx14-Nov-11 16:32 
AnswerRe: convert double to WCHAR Pin
David Crow14-Nov-11 16:54
David Crow14-Nov-11 16:54 
AnswerRe: convert double to WCHAR PinPopular
Peter_in_278014-Nov-11 17:06
professionalPeter_in_278014-Nov-11 17:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.