Click here to Skip to main content
16,005,141 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: detect invalid reference Pin
Mr.Brainley14-Feb-07 5:35
Mr.Brainley14-Feb-07 5:35 
GeneralRe: detect invalid reference [modified] Pin
toxcct14-Feb-07 5:37
toxcct14-Feb-07 5:37 
GeneralRe: detect invalid reference Pin
Mr.Brainley14-Feb-07 5:54
Mr.Brainley14-Feb-07 5:54 
GeneralRe: detect invalid reference Pin
toxcct14-Feb-07 5:58
toxcct14-Feb-07 5:58 
GeneralRe: detect invalid reference Pin
David Crow14-Feb-07 5:43
David Crow14-Feb-07 5:43 
AnswerRe: detect invalid reference Pin
Michael Dunn14-Feb-07 8:57
sitebuilderMichael Dunn14-Feb-07 8:57 
GeneralRe: detect invalid reference Pin
Stephen Hewitt14-Feb-07 11:50
Stephen Hewitt14-Feb-07 11:50 
AnswerRe: detect invalid reference Pin
John R. Shaw14-Feb-07 11:08
John R. Shaw14-Feb-07 11:08 
Mr.Brainley wrote:
How can i detect that the reference has become invalid ?


You can not; a reference just refers to a memory block that is expected to contain data of a given type (the referenced data type). It is up to you, the programmer, to insure that the memory block is valid, as the language does not provide that for you.

If there is a chance that the memory block will become invalid without you knowing it, bad design, then you could try using one of Microsoft’s “IsBad…” functions to check the address referred to by the reference: IsBadWritePtr(&testRef,sizeof(int)). This method, of course, may not work if some other piece of code has allocated that same memory block between the time you freed it and the time you checked the address pointer; which will lead to very hard to find bugs.

Another thing I would like to point out is that just adding another item to the ‘vector’ can cause your reference to become invalid if the ‘vector’ had to reallocate memory in order to expand the memory block size.

Basically, any reference, of the type you provided, should be temporary and only exist in local scope. It should not be ‘static’ or ‘global’, unless the memory it refers to is not released until the application exits.


INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

AnswerRe: detect invalid reference Pin
Stephen Hewitt14-Feb-07 11:46
Stephen Hewitt14-Feb-07 11:46 
AnswerRe: detect invalid reference Pin
Nemanja Trifunovic14-Feb-07 14:34
Nemanja Trifunovic14-Feb-07 14:34 
QuestionC->C++ ported app - unresolved externals Pin
nadiric14-Feb-07 5:04
nadiric14-Feb-07 5:04 
AnswerRe: C->C++ ported app - unresolved externals Pin
nadiric14-Feb-07 5:17
nadiric14-Feb-07 5:17 
GeneralRe: C->C++ ported app - unresolved externals Pin
John R. Shaw14-Feb-07 8:39
John R. Shaw14-Feb-07 8:39 
QuestionRegistering ActiveX ctrls on PC & Embedded platforms Pin
Like2Byte14-Feb-07 3:34
Like2Byte14-Feb-07 3:34 
AnswerRe: Registering ActiveX ctrls on PC & Embedded platforms Pin
Like2Byte14-Feb-07 5:37
Like2Byte14-Feb-07 5:37 
QuestionArray pointer lost Pin
Renato Carvalho14-Feb-07 2:59
Renato Carvalho14-Feb-07 2:59 
AnswerRe: Array pointer lost [modified] Pin
prasad_som14-Feb-07 3:13
prasad_som14-Feb-07 3:13 
GeneralRe: Array pointer lost Pin
David Crow14-Feb-07 3:24
David Crow14-Feb-07 3:24 
GeneralRe: Array pointer lost Pin
prasad_som14-Feb-07 4:30
prasad_som14-Feb-07 4:30 
GeneralRe: Array pointer lost Pin
David Crow14-Feb-07 4:40
David Crow14-Feb-07 4:40 
GeneralRe: Array pointer lost Pin
Renato Carvalho14-Feb-07 4:49
Renato Carvalho14-Feb-07 4:49 
GeneralRe: Array pointer lost Pin
David Crow14-Feb-07 4:55
David Crow14-Feb-07 4:55 
GeneralRe: Array pointer lost Pin
prasad_som14-Feb-07 17:26
prasad_som14-Feb-07 17:26 
GeneralRe: Array pointer lost Pin
Renato Carvalho14-Feb-07 4:31
Renato Carvalho14-Feb-07 4:31 
GeneralRe: Array pointer lost Pin
toxcct14-Feb-07 4:36
toxcct14-Feb-07 4:36 

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.