0

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.

  • 3
    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 Feb 01 '14 at 13:40
  • please provide valid code MyClass obj1 = new MyClass(); is probably not what you wrote. What is cHapticDeviceHandler for? – frans Feb 01 '14 at 13:46
  • 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 Feb 01 '14 at 13:55
  • 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 Feb 01 '14 at 14:13
  • @frans Yes, sorry. I forgot about the * while copying. I've edited my first post. Thank you. – user3260442 Feb 01 '14 at 14:21
  • @user3260442 have you tried stepping into the constructor to see which statement produces the exception, inside the constructor of MyClass. – Stephane Rolland Feb 01 '14 at 14:45
  • @StephaneRolland I can only step into until `handler = new cHapticDeviceHandler()`. If I try to step into the first time, it just step over that line, and the second time give me the access violation exception. – user3260442 Feb 01 '14 at 15:18
  • @user3260442 then it's obvious that your problem is the creation of the second object cHapticDeviceHandler. Try making a simple function that creates two cHapticDeviceHandler one after the other to check that it is really the problem. – Stephane Rolland Feb 01 '14 at 15:24

2 Answers2

0

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?

Rafid
  • 18,991
  • 23
  • 72
  • 108
  • It could be possible, although I've not seen any mention to it in the [docs](http://robot.kaist.ac.kr/haptics/chai3d-2.0.0_Doc/resources/html/classc_haptic_device_handler.html#_details). – user3260442 Feb 01 '14 at 14:00
  • `MyClass* obj1 = new MyClass(); delete obj1; MyClass* obj2 = new MyClass()` will also fail. It shouldn't be crashing after deleting obj1 if this was the case, should it? – user3260442 Feb 01 '14 at 14:19
0

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.

Sebastian Negraszus
  • 11,915
  • 7
  • 43
  • 70
  • If I do something like... MyClass obj1 = new MyClass() and then I delete the object with delete obj1, the problem still happens when using MyClass obj2 = new MyClass(). Wouldn't that mean that this is not the problem? – user3260442 Feb 01 '14 at 14:15
  • @user3260442 MyClass has no destructor and does not delete cHapticDeviceHandler when deleted. So there are still two of them. – Sebastian Negraszus Feb 01 '14 at 14:21
  • I thought that the default destructor was doing that for me. `~MyClass(void) {delete handler;}` would be enough? I'm trying with that but I get the same behaviour. – user3260442 Feb 01 '14 at 14:31