Jump to content

  • Log In with Google      Sign In   
  • Create Account

gretty

Member Since 27 Dec 2009
Offline Last Active May 18 2016 09:56 PM

Posts I've Made

In Topic: Obtain Task Bar Docking Setting: Always returns ABE_LEFT

15 May 2016 - 08:30 PM

Looks like I was using the wrong flag. The following works:

APPBARDATA barData {0};
barData.cbSize = sizeof(APPBARDATA);
UINT_PTR res = SHAppBarMessage(ABM_GETTASKBARPOS, &barData);

In Topic: HINSTANCE & HWND Corrupted when passed around functions

17 April 2016 - 10:10 PM

After extensive debugging I have found the problem and solved it. It's actually quite interesting what went wrong. The problem was here...

struct IEventArgs
{

};

struct WinEventArgs : public IEventArgs
{
    WinEventArgs() = delete; 
    WinEventArgs(const HINSTANCE& hinstance, const HWND& hwnd, const WPARAM& wParam, const LPARAM& lParam) :
        hinstance(hinstance), hwnd(hwnd), wParam(wParam), lParam(lParam)
    {}

    const HINSTANCE& hinstance;
    const HWND& hwnd;
    const WPARAM& wParam;
    const LPARAM& lParam;
};

class EventDelegate
{
public:
        // the problem is here: the parameter should be 'const IEventArgs&'
	typedef std::function<Status(IEventArgs)> EDelegate;

	EventDelegate(EDelegate delegate, GUID gUidContext);

	Status operator()(const IEventArgs& evtArgs)
        {
            return delegate(evtArgs);
        }

private:
	GUID gUidContext;
	EDelegate delegate;
};

I've produced a simple example of the problem that people can reproduce. Whilst I've fixed it, what exactly was going wrong, was it that a copy was being made and I hadn't implemented a copy constructor?

struct IEventArgs {  };

struct WinEventArgs : public IEventArgs
{
	WinEventArgs(const int& a, const int& b) : a(a), b(b) { }
	const int& a;
	const int& b;
};

class Component
{
public:
	void test(const IEventArgs& evtArgs)
	{
		const WinEventArgs& args = static_cast<const WinEventArgs&>(evtArgs);
		printf("a=1 is: %d, b=2 is: %d\n", args.a, args.b);
	}
};

int main()
{
	WinEventArgs args(1,2);
	Component cmp;
	
	// Note Component::test's parameter is 'const IEventArgs&' and that the std::function parameter is just 'IEventArgs'
	std::function<void(IEventArgs)> func = std::bind(&Component::test, cmp, std::placeholders::_1);
	func(args);
	
	// When std::function parameter is 'const IEventArgs&' the cast inside Component::test succeeds
	std::function<void(const IEventArgs&)> func2 = std::bind(&Component::test, cmp, std::placeholders::_1);
	func2(args);
	
	return 0;
}

In Topic: HINSTANCE & HWND Corrupted when passed around functions

17 April 2016 - 12:59 AM

I am specifying a reference; const WinEventArgs& args = static_cast<const WinEventArgs&>(evtArgs);

 

One thing that could be the cause is that; LRESULT CALLBACK Win32App::wndProc(..) is a static function. Therefore when it creates the WinEventArgs object it is also static aswell. The last time the WinEventArgs object is valid is inside EventDelegate::operator()(const IEventArgs & evtArgs) - not a static function.

// Static class function
LRESULT CALLBACK Win32App::wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    WinEventArgs args { hinstance, hwnd, wParam, lParam };
    eventHandler(message, args);  // also static function: IApp::eventHandler(const int & evtId, const IEventArgs & evtArgs)
    ...

In Topic: HINSTANCE & HWND Corrupted when passed around functions

17 April 2016 - 12:43 AM

I've applied the advice given; remove c-style casts, explicitly specify the variable type instead of using auto and resulting in a copy. Unfortunately the crash still occurs when accessing the args object.

 

The "args" parameter has "unable to read memory" showing up in the wParam. This appears to be that your args value is broken, not just the hhnstance or hwnd parameters.

Yes it looks like my args value is broken. Do you have any advice on how I can track down what in my code or design I have done wrong to cause this? I am correctly passing an object of type WinEventArgs to the Win32App::eventHandler() so the problem isn't that I am passing a different subclass IEventArgs.

 

Any advice on how to track down the cause would be greatly appreciated.


In Topic: Software that use Component Based Architecture

25 January 2016 - 09:57 PM



Haha, yes, do you mean like I mention in the second sentence of my original post?

The point is that if you include things like Javabeans and COM, you've covered about 90% of commercial software written between 1998 and 2010.

 

Source?


PARTNERS