Click here to Skip to main content
15,886,137 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Disable a textbox based on combo box choice Pin
lolici8-Jun-17 11:01
lolici8-Jun-17 11:01 
GeneralRe: Disable a textbox based on combo box choice Pin
Jochen Arndt8-Jun-17 21:17
professionalJochen Arndt8-Jun-17 21:17 
QuestionOverload -> operator Pin
_Flaviu6-Jun-17 23:54
_Flaviu6-Jun-17 23:54 
AnswerRe: Overload -> operator Pin
Jochen Arndt7-Jun-17 0:43
professionalJochen Arndt7-Jun-17 0:43 
GeneralRe: Overload -> operator Pin
_Flaviu7-Jun-17 1:56
_Flaviu7-Jun-17 1:56 
AnswerRe: Overload -> operator Pin
Andy Bantly29-Jun-17 6:28
Andy Bantly29-Jun-17 6:28 
QuestionTrying to understand this article on why auto_ptr has been deprecated Pin
swampwiz6-Jun-17 6:45
swampwiz6-Jun-17 6:45 
AnswerRe: Trying to understand this article on why auto_ptr has been deprecated Pin
leon de boer6-Jun-17 9:49
leon de boer6-Jun-17 9:49 
Close but not quite the actual rule goes
An auto_ptr owns the thing/object that it holds a pointer to, and only one auto_ptr may own an object at a time. When you copy an auto_ptr, you automatically transfer ownership from the source auto_ptr to the target auto_ptr. After the copy, only the target auto_ptr owns the pointer and will delete it in due time, while the source is set back to a null state and can no longer be used to refer to the owned object.

The line
doSomething(myTest);
has an implicit copy because you passed in myTest so the ownership goes into doSomething (passing like that is called an auto_ptr sink operation) and myTest gets the null state as per the description. You can't use it afterwards because it got nulled so it's a guaranteed crash if you try to use the pointer.

If you want the auto-pointer back again you should have made doSomething a function returning an auto_ptr and passed it back out, try something like
TestAutoPtr doSomethig(TestAutoPtr myPtr) {
myPtr->m_value = 11;
return myPtr;
}

The higher level code becomes
void AutoPtrTest() {
TestAutoPtr myTest(new Test());
myTest = doSomethig(myTest);  /* We recopy the auto_ptr ownership back */
myTest->m_value = 10;
}

It's just an ownership thing .. only one scope of code can own an autoptr. If you want it back again you need to pass it back otherwise it is assumed it is no longer in use and cleaned up.

The actual crash has nothing to do with the cleanup the null of the auto_ptr is automatic when it copied and it's that which causes the crash. You are attributing the crash to the cleanup which isn't the case. If you could somehow keep the autoptr inside doSomething alive say with multitask code you still couldn't use the original pointer because it's value will be null.

Just use the debugger put a breakpoint on doSomething and single step into it and look at what happens to the original pointer .. your toast from that point on with the original code because it will go to null Smile | :)
In vino veritas


modified 6-Jun-17 16:10pm.

QuestionMessage Closed Pin
2-Jun-17 20:55
Member 132002172-Jun-17 20:55 
Questionbuilding the assimp library Pin
PierreBokma25-May-17 10:01
PierreBokma25-May-17 10:01 
AnswerRe: building the assimp library Pin
Richard MacCutchan25-May-17 20:48
mveRichard MacCutchan25-May-17 20:48 
AnswerRe: building the assimp library Pin
RedDk26-May-17 8:19
RedDk26-May-17 8:19 
QuestionC2011 ‘vc_sttributes::YesNoMaybe’ “ ‘enum’ type redefinition RESOLVED Pin
bkelly1321-May-17 21:12
bkelly1321-May-17 21:12 
AnswerRe: C2011 ‘vc_sttributes::YesNoMaybe’ “ ‘enum’ type redifinition Pin
Richard MacCutchan21-May-17 22:34
mveRichard MacCutchan21-May-17 22:34 
AnswerRe: C2011 ‘vc_sttributes::YesNoMaybe’ “ ‘enum’ type redifinition Pin
Jochen Arndt21-May-17 23:38
professionalJochen Arndt21-May-17 23:38 
AnswerRe: C2011 ‘vc_sttributes::YesNoMaybe’ “ ‘enum’ type redifinition Pin
Randor 22-May-17 0:29
professional Randor 22-May-17 0:29 
GeneralRe: C2011 ‘vc_sttributes::YesNoMaybe’ “ ‘enum’ type redifinition Pin
bkelly1322-May-17 3:44
bkelly1322-May-17 3:44 
GeneralRe: C2011 ‘vc_sttributes::YesNoMaybe’ “ ‘enum’ type redifinition Pin
Randor 22-May-17 5:15
professional Randor 22-May-17 5:15 
GeneralRe: C2011 ‘vc_sttributes::YesNoMaybe’ “ ‘enum’ type redifinition Pin
bkelly1322-May-17 10:28
bkelly1322-May-17 10:28 
GeneralRe: C2011 ‘vc_sttributes::YesNoMaybe’ “ ‘enum’ type redifinition Pin
Randor 22-May-17 11:18
professional Randor 22-May-17 11:18 
GeneralRe: C2011 ‘vc_sttributes::YesNoMaybe’ “ ‘enum’ type redifinition Pin
bkelly1322-May-17 11:53
bkelly1322-May-17 11:53 
GeneralRe: C2011 ‘vc_sttributes::YesNoMaybe’ “ ‘enum’ type redifinition Pin
leon de boer22-May-17 19:49
leon de boer22-May-17 19:49 
GeneralRe: C2011 ‘vc_sttributes::YesNoMaybe’ “ ‘enum’ type redifinition Pin
Richard Andrew x6428-May-17 15:11
professionalRichard Andrew x6428-May-17 15:11 
GeneralRe: C2011 ‘vc_sttributes::YesNoMaybe’ “ ‘enum’ type redifinition Pin
leon de boer28-May-17 16:37
leon de boer28-May-17 16:37 
GeneralRe: C2011 ‘vc_sttributes::YesNoMaybe’ “ ‘enum’ type redifinition Pin
Richard Andrew x6429-May-17 3:24
professionalRichard Andrew x6429-May-17 3:24 

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.