Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does my code have some problems? #5

Open
jonike opened this issue Aug 29, 2018 · 0 comments
Open

Does my code have some problems? #5

jonike opened this issue Aug 29, 2018 · 0 comments

Comments

@jonike
Copy link

jonike commented Aug 29, 2018

Hi, there.

I'm use your Plugin Framework code to study c++ skill.

Below is my test code: main.cpp

#include <iostream>
#include <string>
#include <map>

class IPrint
{
public:
	virtual ~IPrint(){}

	virtual void print() = 0;
};

template <typename T, typename ...T1>
class TemplateClass:public T
{
public:
	typedef T *(*class_t)(T1...);
	typedef std::map<int, class_t> map_int_t;


	virtual ~TemplateClass(){}

	static int Set(int a, class_t b);

	static T* Get(int a, T1... args);

	static void dump();
private:
	static map_int_t mMapIntContainer;
};

template <typename T, typename ...T1>
typename TemplateClass<T,T1...>::map_int_t TemplateClass<T,T1...>::mMapIntContainer;

template <typename T, typename ...T1>
int TemplateClass<T, T1...>::Set(int a, class_t b)
{
	/*test creation*/
	//T* test = b();
	//delete test;
	std::cout << "create:" << a << std::endl;
	std::cout << "map_size:" << mMapIntContainer.size() << std::endl;
	//mMapIntContainer.insert(std::make_pair(a, b)); // @FixMe: if insert anything will be crashed immediately.
	return 0;
}

template <typename T, typename ... T1>
T* TemplateClass<T, T1...>::Get(int a, T1 ... args)
{
	std::cout << "query:" << a << std::endl;
	std::cout << "map_size:" << mMapIntContainer.size() << std::endl;
	auto it = mMapIntContainer.find(a);
	if(it!=mMapIntContainer.end())
	{
		std::cout << "Find*" << std::endl;
		return (it->second)(args...);
	}
	return nullptr;
}


template <typename T, typename ...T1>
void TemplateClass<T, T1...>::dump()
{
	for(auto s :mMapIntContainer)
	{
		std::cout << s.first << ", " << s.second << std::endl;
	}

}
static int count = 0;
template <typename T, typename T1, typename ...T2>
class StaticReg
{
public:
	static T* factory(T2 ... args)
	{
		std::cout << "sublcass creation" << std::endl;
		return new T1(args...);
	}

	StaticReg(){
		std::cout << "StaticReg::StaticReg" << std::endl;
		std::cout << typeid(T).name() << std::endl;
		TemplateClass<T, T2...>::Set(++count, factory);
		std::cout << "count:" << count << std::endl;
	}
};

template <typename T, typename T1, typename ...T2>
class RealTemplateClass: public TemplateClass<T,T2...>
{
public:
	RealTemplateClass(){}
	static StaticReg<T, T1, T2...> hold_obj;
};

template<typename T, typename T1, typename ...T2>
StaticReg<T, T1, T2...> RealTemplateClass<T, T1, T2...>::hold_obj;

class APrint:public RealTemplateClass<IPrint, APrint>
{
public:
	APrint() { std::cout << "APrint:construct" << std::endl; }
	virtual ~APrint(){}

	void print()
	{
		std::cout << "APrint::print" << std::endl;

	}
};

template class RealTemplateClass<IPrint, APrint>;

class BPrint:public TemplateClass<IPrint, BPrint>
{
public:
	BPrint(){std::cout << "BPrint:construct" << std::endl;}
	virtual ~BPrint(){}
	void print()
	{
		std::cout << "BPrint:print" << std::endl;
	}
};

template class RealTemplateClass<IPrint, BPrint>;

int main()
{
	IPrint* aa_ptr = TemplateClass<IPrint>::Get(1);  // return new APrint();
	if(aa_ptr!=nullptr)
	{
		//APrint* a = dynamic_cast<APrint*>(aa_ptr);
		aa_ptr->print();
		delete aa_ptr;
		aa_ptr = nullptr;
	}
	IPrint* bb_ptr = TemplateClass<IPrint>::Get(2);  // return new BPrint();
	if(bb_ptr!=nullptr)
	{
		//BPrint* b = dynamic_cast<BPrint*>(bb_ptr);
		bb_ptr->print();
		delete bb_ptr;
		bb_ptr = nullptr;
	}
	return 0;
}

When i debug this code in VS 2015 x64.

The debugger tell me :

Line 43:mMapIntContainer.insert(std::make_pair(a, b));

Exception thrown: read access violation.
std::_Tree<std::_Tmap_traits<int,IPrint * __ptr64 (__cdecl*)(void),std::less<int>,std::allocator<std::pair<int const ,IPrint * __ptr64 (__cdecl*)(void)> >,0> >::_Root(...) returned 0x8.

Perhaps it is the std::map container is not initialize?

Can you help me fix this problem? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant