Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
very
Jan 25, 2005

I err on the side of handsome.
I've been driving myself nuts for a few days just trying to set up a simple callback. I think I need some Barney-level help. I have two questions:

Firstly, this is the simplest kind of callback class that I have found:

code:
template < class Class, typename ReturnType>
class GUIButtonCallback < Class, ReturnType >
{

   public:

    typedef ReturnType (Class::*Method)(void);

    GUIButtonCallback(Class* _class_instance, Method _method)
    {
       class_instance = _class_instance;
       method         = _method;
    }

    ReturnType operator()()
    {
       return (class_instance->*method)();
    }

    ReturnType execute()
    {
       return operator()();
    }

    private:

      Class*  class_instance;
      Method  method;

};
But this does not compile as-is. It will compile if I remove the < Class, ReturnType > after the class name, and I am not sure why that stuff is there anyway. It looks like a specialization, but why is it specializing in whatever the template specifies?

Second question:

code:
class GUIButton : public GUIControl
{
public:
	GUIButton(void);
	virtual ~GUIButton(void);

	// ...

private:

/* I don't want to specify a type here, because I have no idea what type 
of object this is going to be calling, I just want to be able to call anything! */
	GUIButtonCallback<?????, void> * m_callback;
};
With this scenario, is the callback class that I posted above even going to work? Should I just make it a void pointer? That seems to defeat the purpose and I don't know if that would work anyway.

Adbot
ADBOT LOVES YOU

very
Jan 25, 2005

I err on the side of handsome.

Avenging Dentist posted:

You can't specialize 0 of the template parameters, so it's not a specialization at all. :) (i.e. remove <Class, ReturnType>)
I thought so.

Avenging Dentist posted:

Make a base class for GUIButtonCallback to inherit from and use virtual functions. That is,
Thank you. It seems obvious now.

very
Jan 25, 2005

I err on the side of handsome.
Why is my "this" pointer changing on every line of execution?

My program is crashing because this is changing to something weird like 0x00000001 or 0xffffffff or random junk as I step through the program. The functions worked fine with an earlier build but now I am converting everything over to use wxWidgets and now this is happening all over the place.

I am having a lot of trouble searching as "this" isn't exactly a unique word.

Edit: Actually maybe I have no idea what is going on. Is "this" as it shows up in the debugger supposed to change? Is it showing me the address of this, or something else? I went back to my old implementation and this never changes in any function.

Edit 2: Why are these pointers "expression cannot be evaluated"? They were just newed.

very fucked around with this message at 03:55 on Mar 13, 2008

very
Jan 25, 2005

I err on the side of handsome.
Thanks both of you.

The thing that confuses me is that the entire program runs just fine even though this is pointing all over the place. I just tried adding one feature and one of the libraries I am using was sensible enough to crash when I passed it a this pointer value 0x00000001 and that is why I noticed the problem in the first place.

wxWidgets says to turn off optimization for "bizarre problems" and I guess this is a bizarre problem, so I did that and it works now.

Maybe I need up upgrade to VC++ 2008, but I'm afraid I will have to recompile every library I am using and I'll never get up and running again.

vvv I didn't realize they were on. I don't know why VC++ sets up the Debug configuration just like a release configuration by default when you do an empty project.

very fucked around with this message at 05:07 on Mar 13, 2008

very
Jan 25, 2005

I err on the side of handsome.
I traced the actual problem I am having down to a function in a library that I am using (bullet) where the wrong function is being called. They aren't even the same prototype, or even in the same class hierarchy. The function that is supposed to be called is a simple getCount() type thing that returns an int, but the program actually goes to a different function entirely that returns a void, thus passing on garbage to the caller. The function that gets called takes some arguments by reference, so it is probably doing even more damage by clobbering whatever happens to be in those spots.

Does anybody know what would generally cause the wrong function to be called? I'm using VC++ express 2005.

A friend of mine thinks that the memory where function addresses are stored is getting clobbered somehow. Is that even possible? Would VC++ just let me do that? I put the code that breaks at the very beginning of my program and stepped through it with no other threads running, but it still goes to the wrong function.

I've also since rebuilt the library, but that didn't fix it either.

very
Jan 25, 2005

I err on the side of handsome.

HB posted:

It wouldn't *knowingly* let you do that (not in C++, at least), but it can certainly get corrupted.

What relationship, if any, does the wrong function have with the correct one? Also, double-check the type of the pointer you're using to invoke the method.

Ok. I see something new here:

The caller's type is btPrimitiveManagerBase *, and the function being called is defined in there as virtual int get_primitive_count() const = 0; This obviously shouldn't work, right?

The thing that bothers me is that it does work just fine in an earlier version of my program. The difference being that I switched to using wxWidgets for my interface. I don't know how this is related, but it was such a major overhaul that I can't really pinpoint one thing that made it break.

In the version that works, the caller is still a btPrimitiveManagerBase *, but the function that gets called is in this class:

code:
class btGImpactCompoundShape	: public btGImpactShapeInterface
{
public:
	class CompoundPrimitiveManager:public btPrimitiveManagerBase
	{
		virtual int get_primitive_count() const
		{
			return (int )m_compoundShape->getNumChildShapes();
		}
	/* etc */
	}
/* etc */
}
But I don't see it getting casted to CompoundPrimitiveManager * anywhere. Maybe I should stick a cast in and recompile the library?

very
Jan 25, 2005

I err on the side of handsome.
Thanks for your help. Apparently it was VC++'s retarded defaults for empty projects that got me again. I went through and made sure everything was the same as another project (it wasn't). It works now but I'm not even sure which option was the culprit. I guess I could pull the old one out of SVN if I really want to find out, but I'd rather forget about it.

very
Jan 25, 2005

I err on the side of handsome.
I've tried many C++ IDEs on Linux and I've found eclipse to be the least bad, but I'm not sure if it is better than a basic text editor. Visual Studio is still so much better.

very
Jan 25, 2005

I err on the side of handsome.

Avenging Dentist posted:

emacs is anything but "basic".

I don't think it fits into the category of "basic text editor," no.

Using gdb in emacs can be quite nice... but I don't enjoy editing text in emacs.

very
Jan 25, 2005

I err on the side of handsome.
Your destructors should be called as many times as your constructors.

very
Jan 25, 2005

I err on the side of handsome.
A warning was brought to my attention.

Adbot
ADBOT LOVES YOU

very
Jan 25, 2005

I err on the side of handsome.

Screeb posted:

I thought about a custom allocator, but I know poo poo all about them. I had a read earlier and they're kind of confusing :saddowns:

I only have a bit of experience with this type of thing but I'm pretty sure it will solve your problems as far as speed and fragmentation.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply