![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() ![]() |
Odd WxWidgets Inconsistency?
Okay, with the help of my little friend assert, I was able to see where the code was screwing up.
I still have no clue what this is doing wrong... wxMenu *file_menu = new wxMenu(_(""), wxMENU_TEAROFF);
file_menu->Append(ID_MENU_CLICK, _("E&xit\tAlt-X"), _("Quit this program"));
control -> PopupMenu(file_menu);This identical code works perfectly at an earlier stage in the code, but when it reaches this block, it just crashes without question. I am also 100% sure it is these three lines, since I can comment them and it'll still work. Particularly, it is the third of the three, since it will run fine if I only comment that one. Sorry to be a bother, but this has me absolutely stumped. :o |
|
|
|
|
|
#2 |
|
Programmer
|
Maybe get rid of the spaces in the third line.
contol->PopupMenu(file_menu); But thats most probably rubbish.
__________________
Bite My Shiny Metal Ass Last edited by nemesis; Jul 5th, 2006 at 11:18 AM. |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Is 'control' a properly constituted pointer?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() |
Yeah. I noticed something. I can replace the three lines with
control -> Disable(); And that crashes as well. Which means there must be something wrong with control, not what I'm using it for. I've declared control like so... wxButton *my_button = new wxButton( panel, ID_BUTTON, _("My Button") );Passed it through like so... MyDrop<wxButton*> drop_button1 (my_button, ID_BUTTON, 3, 3); And stored it in MyDrop like so... self control;
public:
MyDrop(const self &ctrl, int ID_CTRL, int x, int y): control (ctrl) {}But it's working properly as a pointer, because I can call methods from control successfully as proven in this section of my code... control -> Connect( ID_MENU_CLICK,
wxEVT_MENU_CLOSE,
(wxObjectEventFunction) &MyDrop :: Close );So what makes that last snippet any different from the one in the original post? Edit: I replaced the "PopupMenu" method (in the original post) with the "Connect" method (in the previous snippet), and it did not crash. Strange .... |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
PopupMenu triggers the showing of the popup menu, are you calling this from within a contructor of the panel? If so that would mean everything isn't properly setup for the panel yet and trying to create a menu on a control who's parent isn't fully intialized will cause problems.
Does it crash right on the last line of the first snippet (control->PopupMenu)m or does it crash somewhere in the PopupMenu call? Have you tried stepping into the PopupMenu call with a debugger? |
|
|
|
|
|
#6 | ||
|
Programming Guru
![]() ![]() |
Quote:
Quote:
If anyone's eager to help (haha, yeah right), drop me a line at dr.sane@gmail.com, and I'll send you the four files. I have reasons I can't publicly display the files. |
||
|
|
|
|
|
#7 | |
|
Newbie
Join Date: Jun 2006
Posts: 20
Rep Power: 0
![]() |
Quote:
Code like: object->method(); is basically preprocessed into something more like: ObjectClass::method( object ); While I don't know if the behavior is considered standard or not, in most compilers (tested with gcc and visc++), you can actually call a method on a null object, so long as the method doesn't actually access any member variables on the object. Virtual methods may also be a problem, though I haven't tested that. So a method like: void ObjectClass::method()
{
printf( "In some method\n" );
}will run, in most compilers, on a null object. It could be that Connect() simply doesn't make use of any of the control's member variables, and so doesn't crash. Again, I don't know if this is part of the C++ standard (in fact, I doubt it is), and it's definitely not something I'd recommend. It *is*, however, something I've seen and reproduced on multiple platforms. Given the evidence, it still seems most likely that control is null. Throw in an assert, and see what happens. EDIT: You may also be better off declaring the template with the raw classname, and using pointers to the class internally. Something weird may be happening with construction/destruction the way you're doing it. It definitely seems like a non-standard way of doing things, at the minimum. template class<self> MyDrop {
private:
self *control;
public:
MyDrop(const self *ctrl, int ID_CTRL, int x, int y): control (ctrl) {}
...
MyDrop<wxButton> drop_button1 (my_button, ID_BUTTON, 3, 3);Though, honestly, I'd make MyDrop just take a wxObject or wxControl instead of going the templated route. I'm not quite sure you're gaining anything with the templates. Last edited by kyoryu; Jul 5th, 2006 at 7:08 PM. |
|
|
|
|
|
|
#8 |
|
Programming Guru
![]() ![]() |
No, it's definitely not null. The "Connect" method binds an event to a function. The location where the "PopupMenu" method crashes, is inside of the function that was bound to the event. By this logic, "Connect" worked and "PopupMenu" did not.
|
|
|
|
|
|
#9 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#10 | |
|
Hobbyist
Join Date: Sep 2005
Posts: 266
Rep Power: 4
![]() |
Quote:
#include <iostream>
#include <string>
template <typename T>
class MyDrop
{
public:
MyDrop(const T& ctrl)
: control_(ctrl) {}
const int Size(void) const { return control_.size(); }
private:
const T& control_;
};
template <typename T>
class MyDrop<T*>
{
public:
MyDrop(const T* ctrl)
: control_(ctrl) {}
const int Size(void) const { return control_->size(); }
private:
const T* control_;
};
int main (void)
{
std::string txt("Hello!");
std::string* txt_ptr = &txt;
MyDrop<std::string> drop1(txt);
MyDrop<std::string*> drop2(txt_ptr);
std::cout << "txt = " << txt << "\n";
std::cout << "drop1.Size = " << drop1.Size() << "\n";
std::cout << "drop2.Size = " << drop2.Size() << "\n";
std::cin.get();
return 0;
} |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|