Really shameless pointer abuse
Posted on 2007-10-10
OK, this one's gonna be difficult to explain, but I'll do my best . . .
My company writes bank software. The software has a marketing programs feature which will pop up a marketing program eligibility dialog to the teller when they pull up a customer's account. The teller pitches the program to the customer, and has the option to click accept, decline, or follow-up. If the teller clicks accept or follow-up, the program creates a sales lead in the system. Later on, when a branch manager is reviewing the sales leads, he/she can add notes to each one if they wish.
Right now I am working to extend this feature so that the teller can also enter notes while the lead is being generated. It has to be on the marketing popup, because tellers do not have access to the sales leads screen, and once the teller hits accept or follow-up, the box goes away.
The goal here is to simply recycle the the notes dialog from the sales lead screen. But since I can't add a note to a sales lead that doesn't exist yet, I have to call the notes dialog, "steal" the info out of it so it can be bent to my own evil desires, and then kill it before the code to add the note to the database gets executed (which is easily accomplished by having the notes dialog end if the sales lead ID is -1). When the teller hits accept, then the marketing popup is supposed to create the new sales lead, get the ID, and then attach the note to it all in one shot.
So here's what I've attempted to do:
I added an "Add Note..." button and gave the marketing popup a CEdit *noteTextBox member. Then, I call the notes dialog and pass it the address of the noteTextBox member (¬eTextBox) to its Init() function (which I extended to accept a pointer to a CEdit box as one of its arguments).
Now, when the Init() function of the notes dialog runs, it takes the address of its own CEdit box (which is already defined), and assigns it to the reference it got from the marketing popup. The function looks like this:
... in the marketing popup,
.. in the notes dialog,
pNotesTextBox = &m_notesEdit; //m_notesEdit is a private CEdit object
By my reasoning, the marketing popup's edit box should now be pointing to the same place as the notes dialog's edit box, right? Furthermore, changes in the notes dialog's edit box should be automatically visible from the marketing popup, right??
Well, as soon as the marketing popup does a notesTextBox.GetWindowText(string), the program pukes with a debug assertion error. I'm not sure why, but I have a few theories:
- The note dialog's m_notesEdit member is private and I am violating encapsulation (this means the compiler is smarter than I am, yet still not smart enough to warn me about it).
- The pointer is getting mangled somehow between the two objects (one's a CEdit, and the other's a CEdit*)
- At some point before I do the GetWindowText(), the notes dialog is being destroyed (along with all its members), making the pointer no longer valid. Does EndDialog() call the destructor?
- My thinking is totally wrong. It's completely insane to have two members from different objects pointing to the same place and I should try doing this some other way.
- A fifth option which I haven't considered
Anyone have any ideas as to what's going wrong here and how to fix it? It would be very much appreciated. If you can help me out with this, then me luv u long time!