Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jul 3rd, 2007, 10:29 AM   #1
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
multiple inheritance problem

heya,

im writing a plugin for maya, and well ive run into a problem that appears to be multiple inheritance...

what would you think the problem was:

#pragma once

#include <maya/MPxFileTranslator.h>

class Parser {
	public:
		Parser() {
			cout << "Parser::Parser()" << endl;
			file = 0;
			cout << "end Parser::Parser()" << endl;
		}
		virtual ~Parser() {
			cout << "Parser::~Parser()" << endl;
			Cleanup();
			cout << "end Parser::~Parser()" << endl;
		}
		void Cleanup() {
			cout << "Parser::Cleanup()" << endl;
			delete[] file;
			file = 0;
			cout << "end Parser::Cleanup()" << endl;
		}
	private:
		char *file;
};

class ParserTranslator : public Parser,public MPxFileTranslator {
	public:
		ParserTranslator() : Parser() {
			cout << "ParserTranslator::ParserTranslator()" << endl;
			cout << "end ParserTranslator::ParserTranslator()" << endl;
		}
		~ParserTranslator() {
			cout << "ParserTranslator::~ParserTranslator()" << endl;
			cout << "end ParserTranslator::~ParserTranslator()" << endl;
		}
		static void *creator() {
			return new ParserTranslator;
		}
	private:
		//
};

Parser::Parser()
end Parser::Parser()
ParserTranslator::ParserTranslator()
end ParserTranslator::ParserTranslator()
ParserTranslator::~ParserTranslator()
end ParserTranslator::~ParserTranslator()
Parser::~Parser()
Parser::Cleanup()
end Parser::Cleanup()
end Parser::~Parser()
*** glibc detected *** /usr/aw/maya/bin/maya.bin: double free or corruption (fasttop): 0x0a7260f0 ***

hope someone can help! im confused!

this shouldnt be happening???

EDIT:

just removed the comments so you might be more inclined to read it!

#pragma once

#include <maya/MPxFileTranslator.h>

class Parser {
	public:
		Parser() {
			file = 0;
		}
		virtual ~Parser() {
			Cleanup();
		}
		void Cleanup() {
			delete[] file;
			file = 0;
		}
	private:
		char *file;
};

class ParserTranslator : public Parser,public MPxFileTranslator {
	public:
		ParserTranslator() : Parser() {}
		~ParserTranslator() {}
		static void *creator() {
			return new ParserTranslator;
		}
	private:
		//
};
rwm is offline   Reply With Quote
Old Jul 4th, 2007, 3:44 AM   #2
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
i got a reply on the maya boards saying multiple inheritance is generally a bad idea in this case... any suggestions?
rwm is offline   Reply With Quote
Old Jul 4th, 2007, 10:33 AM   #3
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
ok, i wrote a custom wrapper so i could overload new/delete operators...

typedef Wrapper<int> Int;

class Parser {
	public:
		Parser() {
			p = 0;
		}
		Parser(int i) {
			p = new Int(i);
			cout << *p << endl;
		}
		virtual ~Parser() {
			Cleanup();
		}
		void Cleanup() {
			delete p;
			p = 0;
		}
	private:
		Int *p;
};

class ParserTranslator : public Parser,public MPxFileTranslator {
	public:
		ParserTranslator() : Parser(101) {
		}
		~ParserTranslator() {
		}
		static void *creator() {
			return new ParserTranslator;
		}
	private:
		//
};

and the output is

allocated 4 bytes (0x9d9d8c8)
101
deallocating (0x9478f70)
*** glibc detected *** /usr/aw/maya/bin/maya.bin: double free or corruption (fasttop): 0x09478f70 ***

its obviously freeing the wrong memory, but why?

if we look at the order of destruction it appears to be:

~ParserTranslator
~Parser

then i assume

~MPxFileTranslator

so i suppose that its destructing in the wrong order then?

any suggestions? please help!
rwm is offline   Reply With Quote
Old Jul 4th, 2007, 10:46 AM   #4
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
ive tested a similar inheritance in standard code, in this case i dont know what BaseB does (substitute for MPxFileTranslator) - in this case the only thing i can think of is that there is some sort of error in MPxFileTranslator???

#include <iostream>
using namespace std;

...

typedef Wrapper<int> Int;

class BaseA {
	public:
		BaseA() {
			p = 0;
		}
		BaseA(int i) {
			p = new Int(i);
			cout << *p << endl;
		}
		virtual ~BaseA() {
			Cleanup();
		}
		void Cleanup() {
			delete p;
			p = 0;
		}
	private:
		Int *p;
};

class BaseB {
	public:
		BaseB() {}
		~BaseB() {}
};

class Derived : public BaseA,public BaseB {
	public:
		Derived() : BaseA(101) {}
		~Derived() {}
};

int main() {
	//
	Derived der;

	return 0;
}

and the result is

allocated 4 bytes (0x8351008)
101
deallocating (0x8351008)

am i missing a key point here? or am i doing the stupidest thing on this planet?
rwm is offline   Reply With Quote
Old Jul 5th, 2007, 4:55 AM   #5
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
ah well i just found out that the plugin resides in its own memory space i.e. its a dll/so and my Parser class resides in its own memory space... hence invalid memory being freed...

i have a question, how can you make sure that memory of base classes resides in the dll/so space?

please help! puuuuuuhhhhhlllllllllllleeeeeeeeeeeaaaaaaaaaaaaaasssssssssssssseeeeeee!
rwm is offline   Reply With Quote
Old Jul 5th, 2007, 5:09 AM   #6
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
i found this on google:


To overcome the seperate heap problem you can make the destructor and all other functions that allocate/free memory of those objects that will be used across dll boundaries virtual. That way the object's "original" destructor/function will be called via the vtbl rather than the local one. So all will happen on the heap where the object was allocated.


class Parser {
	public:
		Parser() {
			p = 0;
		}
		Parser(int i) {
			p = new Wrapper<int>(i);
			cout << *p << endl;
		}
		virtual ~Parser() {
			Cleanup();
		}
		virtual void Cleanup() {
			delete p;
			p = 0;
		}
	private:
		Int *p;
};

same problem...
rwm 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
max multiple of 3 xenop Software Design and Algorithms 2 Nov 7th, 2006 1:28 AM
dealing with multiple forms Booooze C# 12 May 12th, 2006 6:12 PM
Inheritance problem OpenLoop C# 6 Oct 3rd, 2005 12:50 AM
Multiple http-requests are stuck MereMortal C++ 0 May 4th, 2005 4:08 AM
Multiple inheritance Shatai C++ 5 Apr 30th, 2005 8:16 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:48 AM.

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