• Advertisement

Archived

This topic is now archived and is closed to further replies.

VC6: Access Violation

This topic is 5981 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''d really be grateful if anyone could help here: I''ve been pulling my hair out over this for hours now. I have a class which loads model data from a file. Inside the class are three pointers triangledatastruct* ptrToTriangles vertexdatadstruct* ptrToVertices texcoorddatastruct* ptrToTexCoords As you may have guessed, these are pointers to a data structure. All of those have been declared public variables inside the class. Also inside this class is a load function, which uses malloc() to allocate memory to each pointer, depending on what is required at runtime. e.g. ptrToTrianges = malloc(numberoftriangles * sizeof(triangledatastruct)) This seems to work perfectly. There''s another function in that class we''ll call render(). If render() tries to access any data inside these structures then I get an 0xC0000005: Access Violation error. e.g. int tx = ptrToVertices[2].avaluehere I''ve used almost entirely the same code in a DOS Console application and it worked ok. I don''t suppose anyone knows what''s wrong? Cheers, E

Share this post


Link to post
Share on other sites
Advertisement
When you malloc() like in the old days, I used to typecast it without fail. I wonder if that''s a prob. Like:

ptrToTrianges = (triangledatastruct*)malloc(numberoftriangles * sizeof(triangledatastruct));

Or... are you by any chance passing triangledatastruct* ptrToTriangles to a member function that allocate memory for it? I once bumped into the error doing that, because I was passing the pointer to the structure when I was supposed to pass the pointer to the pointer to the structure (structure**).

I''m just making wild guesses. The code you''ve shown above seems to have no problems other than typecast (unless you are running out of memory, which I doubt very much).

Share this post


Link to post
Share on other sites
Thanks for your reply,

I am typecasting the result from malloc, I just didn''t put it in because I was concerned about filling up my post with loads of stuff you wouldn''t want to see.

The other thing is that I''m not actually passing ptrToTriangles etc. to the member functions. Since they should be global to the whole class(?) I can just refer to them by name. I think.

Or maybe I can''t?

E

Share this post


Link to post
Share on other sites
Yeah, if you declared it within the class then its scope is class-wide.

I doubt you would get much help on this other than few miss-placed (or well-placed if lucky) guesses. At least I don''t see anything elementary there. You might get more help if you showed Load function and the function that''s causing probs. It might be to do with another variable messing up the size at malloc or something. Who knows.

Share this post


Link to post
Share on other sites
Argh... this is annoying.

Oh well, here''s the loading code:
  void k3d_object::import_k3d(char* filename, float scale) {
// variable declarations

ifstream fin(filename,ios::binary);
ImportFrameHeaderRecord theframeheader;
ImportVertexDataRecord invertex;
ImportTexCoordDataRecord intexcoord;
ImportTriangleDataRecord intriangle;
int counter = 0;
//


fin.read ((char*)(&theframeheader),sizeof(ImportFrameHeaderRecord));

arraycpy(&theframeheader.frame_name[0],&frames.frame_name[0],30);

frames.number_of_triangles = theframeheader.number_of_triangles;
frames.number_of_texture_coords = theframeheader.number_of_texture_coords;
frames.number_of_vertices = theframeheader.number_of_vertices;

frames.vertices = (VertexDataRecord*)malloc(frames.number_of_vertices * sizeof(VertexDataRecord));
counter=0;
while (counter < theframeheader.number_of_vertices) {
fin.read ((char*)(&invertex),sizeof(ImportVertexDataRecord));
frames.vertices[counter].values[0] = invertex.x / scale;
frames.vertices[counter].values[1] = invertex.y / scale;
frames.vertices[counter].values[2] = invertex.z / scale;
counter++;
}

frames.texture_coords = (TextureCoordinateRecord*)malloc(frames.number_of_texture_coords * sizeof(TextureCoordinateRecord));
counter=0;
while (counter < theframeheader.number_of_texture_coords) {
fin.read ((char*)(&intexcoord),sizeof(ImportTexCoordDataRecord));
frames.texture_coords[counter].s = intexcoord.s;
frames.texture_coords[counter].t = intexcoord.t;
counter++;
}

frames.triangles = (TriangleDataRecord*)malloc(frames.number_of_triangles * sizeof(TriangleDataRecord));
counter=0;
while (counter < theframeheader.number_of_triangles) {
fin.read ((char*)(&intriangle),sizeof(ImportTriangleDataRecord));
frames.triangles[counter].vertices[0] = intriangle.v1;
frames.triangles[counter].vertices[1] = intriangle.v2;
frames.triangles[counter].vertices[2] = intriangle.v3;
frames.triangles[counter].texture_coords[0] = intriangle.t1;
frames.triangles[counter].texture_coords[1] = intriangle.t2;
frames.triangles[counter].texture_coords[2] = intriangle.t3;
arraycpy(&intriangle.material[0],&frames.triangles[counter].material[0],30);
counter++;
}

// TODO:

// Add code to calculate the normals for these triangles


fin.close();
}


Frames is just a structure within the class which holds the rest of the data. Hmm... I wonder if this might be the problem???

As for code which triggers the problem:
  
void k3d_object::render() {
// variable declarations

float tx;
//

tx = frames.vertices[1].values[0];
}


Anything like that will do it.

Hope this helps.

E

Share this post


Link to post
Share on other sites
quote:

As you may have guessed, these are pointers to a data structure. All of those have been declared public variables inside the class.


public variables in a class are symptoms of bad design.

I see many "magic numbers" in your code: the 0, 1, 2 for vertices, though we all know what they are, and the 30 in arraycpy. Magic numbers are also symptoms of bad design.

I don''t know what arraycpy is. Are you sure it works?

You should be storing the lengths of these arrays. Are you sure they''re as long as you think they are?

As for the code that triggers the problem:
tx = frames.vertices[1].values[0];

If this causes you an access violation it means that either:
- frames is invalid.
- vertices is invalid or is a valid array of length less than 2.
- values is not a valid pointer.

You need to look at these values in the debugger and figure out which of these is true.

Share this post


Link to post
Share on other sites
>public variables in a class are symptoms of bad design.
I''ll resist the temptation to get defensive here and merely ask a) why and b) what would you suggest I use instead?

Bare in mind that this whole class is based on storing the data, with a few manipulation routines tied in. I really can''t see the problem. Surely, that''s what C++ is all about?

>I don''t know what arraycpy is. Are you sure it works?
Yep.

>You should be storing the lengths of these arrays.
I am. The number_of_blahblah variables happens to refer to the number of items to be loaded from the file. It''s no cooincidence that if number_of_vertices is 5, then space for 5 vertex records will be allocated and 5 vertex records loaded in from disk.

>Are you sure they''re as long as you think they are?
Yes.

>You need to look at these values in the debugger and figure out
>which of these is true.
Well. You''re certainly right about that one. Unfortunately, I''m not too familiar with the VC++ debugger so it didn''t actually occur to me to use it.

Thanks for your help.

E
E

Share this post


Link to post
Share on other sites
quote:
Original post by Eight
>public variables in a class are symptoms of bad design.
I''ll resist the temptation to get defensive here and merely ask a) why and b) what would you suggest I use instead?

Bare in mind that this whole class is based on storing the data, with a few manipulation routines tied in. I really can''t see the problem. Surely, that''s what C++ is all about?


Convention is that structs have public data members, classes do not. The generally-accepted style is that classes in C++ hide their data, where it can only be accessed through member functions. Any C++ book will tell you this.

If you''re just using data structures with a few manipulation routines, use struct instead. They work the same way, but the convention is that you use structs where all the data members are public.

quote:

>You should be storing the lengths of these arrays.
I am. The number_of_blahblah variables happens to refer to the number of items to be loaded from the file. It''s no cooincidence that if number_of_vertices is 5, then space for 5 vertex records will be allocated and 5 vertex records loaded in from disk.


Excellent. Drop a few asserts in your code. Whenever you access array element X, assert that your array size is at least X+1. Assert is your friend.

Share this post


Link to post
Share on other sites
Thanks for all the advice so far. I''ve managed to track down the source of the problem but I''m not sure how to fix it.

Somewhere along the line the program stopped being able to allocate enough memory for the ptrTo_variables. The debugger reports that...
HEAP[k3dtest.exe]: Invalid allocation size - fffd99c0 (exceeded 7ffdefff)

I''ve tried changing the size of the heap by adding (e.g. /heap:4096) to the linker options but it hasn''t had any effect.

Any thoughts?

Cheers.

Share this post


Link to post
Share on other sites
ptrToTrianges = (triangledatastruct*)malloc((numberoftriangles+1) * sizeof(triangledatastruct));

might fix the problem, looks pretty obvious to me, it would seem somewhere your running over the boundary of the variable, all those while loops, btw for loops are far better for your situation

Share this post


Link to post
Share on other sites
Thanks for trying Marvin, but it made no difference.

I was using for loops but changed to while() for some reason. Can''t remember now... (well it was yesterday).

E

Share this post


Link to post
Share on other sites
ARRRRGGGGGHHHHHHH....

Found the source of the problem. Windows trashed my data file and my program didn''t pick that up.

There''s a lesson learnt the hard way. Thanks to everyone who tried to help, I really appreciate you all taking the time.

Cheers,

Eight

Share this post


Link to post
Share on other sites

  • Advertisement