Memory problems
-
I posted something similar to this earlier, but I think I'm getting closer to the problem. I wrote my own class, CFAFRecord, that is a derivative of CObject. When someone presses the button, I do something similar to
my_ObjectArray.SetSize(10);
CFAFRecord my_Record;
my_Record.name = m_Name;
my_Record.address = m_Address;
my_ObjectArray.SetAt(0, &my_Record);my_ObjectArray in the example is a CObArray that is a member variable of my dialog. Later, when I try to access, I try
CFAFRecord * my_Record = *(CFAFRecord *)my_ObjectArray.GetAt(0);
m_Name = (*my_Record).GetName();When I try that last line, I get an access violation, looks like a memory access error. I think I'm handling the memory wrong somewhere, maybe in the first functiion, when I use the SetAt, I pass the pointer to my_Record and my_Record is probably destoyed at the end of the function. If that's the case, how can I can I fix this? Danny The stupidity of others amazes me!
-
I posted something similar to this earlier, but I think I'm getting closer to the problem. I wrote my own class, CFAFRecord, that is a derivative of CObject. When someone presses the button, I do something similar to
my_ObjectArray.SetSize(10);
CFAFRecord my_Record;
my_Record.name = m_Name;
my_Record.address = m_Address;
my_ObjectArray.SetAt(0, &my_Record);my_ObjectArray in the example is a CObArray that is a member variable of my dialog. Later, when I try to access, I try
CFAFRecord * my_Record = *(CFAFRecord *)my_ObjectArray.GetAt(0);
m_Name = (*my_Record).GetName();When I try that last line, I get an access violation, looks like a memory access error. I think I'm handling the memory wrong somewhere, maybe in the first functiion, when I use the SetAt, I pass the pointer to my_Record and my_Record is probably destoyed at the end of the function. If that's the case, how can I can I fix this? Danny The stupidity of others amazes me!
Thought so, you have now posted the offending code.
my_ObjectArray.SetSize(10);
CFAFRecord my_Record;
my_Record.name = m_Name;
my_Record.address = m_Address;
my_ObjectArray.SetAt(0, &my_Record);my_Record
goes out of scope and is destroyed, so that the address that is stored inmy_ObjectArray
now points to useless memory, thus your access violations.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it!
-
I posted something similar to this earlier, but I think I'm getting closer to the problem. I wrote my own class, CFAFRecord, that is a derivative of CObject. When someone presses the button, I do something similar to
my_ObjectArray.SetSize(10);
CFAFRecord my_Record;
my_Record.name = m_Name;
my_Record.address = m_Address;
my_ObjectArray.SetAt(0, &my_Record);my_ObjectArray in the example is a CObArray that is a member variable of my dialog. Later, when I try to access, I try
CFAFRecord * my_Record = *(CFAFRecord *)my_ObjectArray.GetAt(0);
m_Name = (*my_Record).GetName();When I try that last line, I get an access violation, looks like a memory access error. I think I'm handling the memory wrong somewhere, maybe in the first functiion, when I use the SetAt, I pass the pointer to my_Record and my_Record is probably destoyed at the end of the function. If that's the case, how can I can I fix this? Danny The stupidity of others amazes me!
bugDanny wrote:
CFAFRecord my_Record;
Should be:
CFAFRecord *my_Record = new CFAFRecord;
...
my_ObjectArray.SetAt(0, my_Record);
"Take only what you need and leave the land as you found it." - Native American Proverb
-
bugDanny wrote:
CFAFRecord my_Record;
Should be:
CFAFRecord *my_Record = new CFAFRecord;
...
my_ObjectArray.SetAt(0, my_Record);
"Take only what you need and leave the land as you found it." - Native American Proverb