Access violation , help needed!!

Hi all,

I am writing a program to handle a haptic device using opengl and a library chai3d. It's in c++. I've been haviing an error in program which says:

"Unhandled exception at 0x0042ef79 in 25-cubic.exe: 0xC0000005: Access violation reading location 0x00000134."


When I tried to debug it's basically an issue with particular source file, which is a function with pointers as arguments. But I cant able to find what exactly is the matter.

I'm posting that particular source here:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "chai3d.h"

void CreateWorld(cWorld* world, cCamera* camera, cLight* light,cGenericObject* rootlabels)
{

    // Intialization of  world 


    world = new cWorld();
	world->setBackgroundColor(0.0, 0.0, 0.0);
	camera = new cCamera(world);
    world->addChild(camera);
	camera->set( cVector3d (0.5, 0.0, 0.0),    // camera position (eye)
                 cVector3d (0.0, 0.0, 0.0),    // lookat position (target)
                 cVector3d (0.0, 0.0, 1.0));   // direction of the "up" vector

	camera->setClippingPlanes(0.01, 10.0);

    // create a light source and attach it to the camera
    light = new cLight(world);
    camera->addChild(light);                   // attach light to camera
    light->setEnabled(true);                   // enable light source
    light->setPos(cVector3d( 2.0, 0.5, 1.0));  // position the light source
    light->setDir(cVector3d(-2.0, 0.5, 1.0));  // define the direction of the light beam


	// create a node on which we will attach small labels that display the
    // position of each haptic device
    rootlabels = new cGenericObject();
    camera->m_front_2Dscene.addChild(rootlabels);

    // create a small label as title
    cLabel* titleLabel = new cLabel();
    rootlabels->addChild(titleLabel);

    // define its position, color and string message
    titleLabel->setPos(0, 30, 0);
    titleLabel->m_fontColor.set(1.0, 1.0, 1.0);
    titleLabel->m_string = "Haptic Device Pos [mm]:";
//exit (0);
}


I very much appreciate if some one could give me a feedback. I'm really stuck here!.

Last edited on
One of your pointers is bad.

By the looks of the address being accessed (0x00000134), it's likely that one of them is null.

When the program crashes, the debugger should snap you to the exact line that is the culprit. From there you can examine the contents of each pointer to see which one is null.


EDIT:

Actually now that I look at this code... you are passing the pointers into this function by value -- which is meaningless because you discard their incoming values. They might as well be local to the function.

On top of that you have memory leaks because you never delete all these objects you've new'd.

Are you intending for the created objects to be visible outside this function? If yes, then you'll have to pass the pointers by reference:

void CreateWorld(cWorld*& world, cCamera*& camera, cLight*& light,cGenericObject*& rootlabels)

(Note the added &s)

Also, consider using smart pointers so you don't have to worry about manual cleanup of these objects.

Last edited on
Hi Disch!, Thank you so much for your feedback. I tried to pass pointers by ref as you mentioned. It's now proceeding further but now it's creating problem somewhere else again other access violation. I'm wondering how to modify this with using smartpointers?
It's hard to give an exact demonstration of how you'd apply smart pointers to this particular problem without seeing the entirely of the code, but basically you can replace the pointer of whoever owns the object with a smart pointer so that you do not have to manually delete it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// assuming main() is the owner of your world

// without smart pointers:
int main()
{
    World* world( new World );

    // use the world

    delete world; // <- have to delete it to avoid memory leaks
}


// with smart pointers
int main()
{
    std::unique_ptr<World> world( new World );

    // use the world

}  // <- automatically deleted, we don't have to worry about it 


The automatic cleanup makes it all but impossible for the object to leak.



This of course does not mean you can blindly replace ALL pointers with unique_ptrs. It all depends on who the owner is.
Topic archived. No new replies allowed.