0
votes

I'm facing an Illegal Access error, but I'm not sure whats happening in my code...

I have a class like this:

class MyClass
{
    cHapticDeviceHandler* handler;
public:
    MyClass(void){handler = new cHapticDeviceHandler();}
    ~MyClass(void){delete handler;}
    cHapticDeviceHandler* getHandler() {return handler;}
};

If I create a function like this my code just works. I can create a new object and use that getHandler() method to use the cHapticDeviceHandler* without problems.

function A(){
   MyClass* obj1 = new MyClass();
   ...
}

However, if I try doing something like this...

function B(){
   MyClass* obj1 = new MyClass();
   MyClass* obj2 = new MyClass();
}

The first obj1 is created without problems, but the second one just crash with an Illegal Access error while executing the constructor.

If I'm not wrong, when you create new objects from a class, their attributes are different so each object has his own attributes. With this in mind, I pressume that those cHapticDeviceHandler pointers are different in the two objects, so I can't understand why doing a new in the first place works, and doing it again just won't work.

I'm pretty sure I'm doing something really wrong and embarrasing buuuut... I can't find where is the problem :$

Could anyone give me a hint please? I'm forced to use Visual Studio 2008 and I'm using CHAI3D, just in case that's important.

2
Welcome on SO. Best hint I could give you is to try to debug your application and see what is the line that gives a problem. Use breakpoints and step by step. We don't have all the code here, and what you show doesn't look like a source of the problem. Another remark: you allocate memory with new but you don't deallocate it, that causes a memory leak. - Stephane Rolland
please provide valid code MyClass obj1 = new MyClass(); is probably not what you wrote. What is cHapticDeviceHandler for? - frans
I'm using the debugger and the line that causes the problem is this one "MyClass(void){handler = new cHapticDeviceHandler();}" when being called by the second object creation. Thank you about the memory leak. @frans: Yes... that's valid code, that's exactly how is written in my code. The cHapticDeviceHandler is a class from CHAI3D: "This class implements a manager which lists the different devices available on your computer and provides handles to them." - user3260442
so didn't you want to write MyClass *obj1 = new MyClass();? Otherwise you would write a Pointer to an instance, which is at least not very common. - frans
@frans Yes, sorry. I forgot about the * while copying. I've edited my first post. Thank you. - user3260442

2 Answers

0
votes

My guess is that you cannot create more than one cHapticDeviceHandler instance. So when you create the first MyClass object, you are fine, but when you create the second MyClass object, it will try to instantiate another cHapticDeviceHandler and this will not be allowed. Could it be that you only have one device, and thus cannot have two handlers for it?

0
votes

Without more information, I can only guess. The problem seems to be that you try to create more than one instance of cHapticDeviceHandler. Maybe it does not support creating more than one instance, e.g. because it uses some static data or uses exclusive resources (see device driver)?

Consult the documentation or code of the cHapticDeviceHandler class to verify this.

If you really can only create one such object you might solve your problem by providing a singleton factory for the object.