Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 5th, 2006, 10:51 AM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,086
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
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
Sane is online now   Reply With Quote
Old Jul 5th, 2006, 11:01 AM   #2
nemesis
Programmer
 
nemesis's Avatar
 
Join Date: Aug 2005
Location: Bristol, England
Posts: 71
Rep Power: 4 nemesis is on a distinguished road
Send a message via MSN to nemesis
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.
nemesis is offline   Reply With Quote
Old Jul 5th, 2006, 12:10 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jul 5th, 2006, 12:22 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,086
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
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 ....
Sane is online now   Reply With Quote
Old Jul 5th, 2006, 12:59 PM   #5
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
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?
Game_Ender is offline   Reply With Quote
Old Jul 5th, 2006, 3:17 PM   #6
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,086
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Quote:
Originally Posted by Game_Ender
Does it crash right on the last line of the first snippet (control->PopupMenu)m or does it crash somewhere in the PopupMenu call?
Quote:
Originally Posted by Sane
Particularly, it is the third of the three, since it will run fine if I only comment that one.
I am able to take the same control and apply a drop menu to it outside of the MyDrop class, so it doesn't seem like a problem with initialization either.

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.
Sane is online now   Reply With Quote
Old Jul 5th, 2006, 6:57 PM   #7
kyoryu
Newbie
 
Join Date: Jun 2006
Posts: 20
Rep Power: 0 kyoryu is on a distinguished road
Quote:
Originally Posted by Sane
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 ....
Remember that C++ is basically one big preprocessor hack.

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.
kyoryu is offline   Reply With Quote
Old Jul 5th, 2006, 7:06 PM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,086
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is online now   Reply With Quote
Old Jul 5th, 2006, 7:19 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
Remember that C++ is basically one big preprocessor hack.
While metaphors are great teaching tools, I think you might want to consider that this forum has a fair number of novices who will interpret that literally.
__________________
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
DaWei is offline   Reply With Quote
Old Jul 6th, 2006, 12:31 PM   #10
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
Quote:
Originally Posted by kyoryu
You may also be better off declaring the template with the raw classname, and using pointers to the class internally.
The "non-standard" (as you say) use of 'MyDrop<wxButton*>' is my fault from another thread, I believe. However, T<T*> is perfectly fine where partial specialization is used:

#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;
}
Cache is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:26 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC