![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
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:
//
}; |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
i got a reply on the maya boards saying multiple inheritance is generally a bad idea in this case... any suggestions?
|
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
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! ![]() |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
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? |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
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! ![]() |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
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... |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |