Archived

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

Access violation by memeset and fopen

This topic is 5269 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

When I compile my code I get an access violation. The debugger seems to think that it is caused by both memset and fopen because it wants to see their code. Here are my definitions
#ifndef GUI_H_
#define GUI_H_

#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdio.h>
#include <string.h>

struct GUI_TEXT_BOX
{
	void (*On_Action)(void);
	int ID;
	char buffer[255];
	char command[255];
	int x,y,width,height;        //width and height are in characters not pixels
	int bufferPosition;
};
struct GUI_BUTTON
{
	void (*On_Action)(void);
	int ID;
	char *Caption;
	int x,y;
};

class GUI_OBJECTS
{
private:
	GUI_TEXT_BOX *texts;
	GUI_BUTTON *buttons;
	GUI_TEXT_BOX *temp_texts;
	GUI_BUTTON *temp_buttons;
	int numButtons;
	int numTexts;
	int focused_text_id;
	
	void guiPrint(char *fmt,int x, int y);
	
	unsigned int base;
public:
	HDC hDc;
	void AddToTextsStack(GUI_TEXT_BOX text);
	void AddToButtonsStack(GUI_BUTTON button);
	void FocusObject(int x,int y);
	void AddToTextBuffer(char c);
	void RenderGUI();
	void BuildFont(int height,int width, char *typeface);
	
};

class GUI
{
public:
	void Update(int x,int y,int b,char c);
	void SetUpText(void (*On_Action)(void),int ID,char *Command,int x,int y,int width,int height);
	void SetUpButton(void (*On_Action)(void),int ID,char *Caption,int x,int y);
	void InitGUI(HDC hDc);
	

private:
	
	GUI_OBJECTS objects;
	

};


	

#endif

 
and here is the code
void GUI_OBJECTS::AddToTextsStack(GUI_TEXT_BOX text)
{
	if(numTexts>0)
	{
		temp_texts=new GUI_TEXT_BOX[numTexts];

		memset(temp_texts,0,sizeof(GUI_TEXT_BOX)*numTexts);
		int i;
		for (i=0;i=texts[i];
		}
		
		delete texts;
		numTexts+=1;
		texts = new GUI_TEXT_BOX[numTexts];
		memset(texts,0,sizeof(GUI_TEXT_BOX)*numTexts);
		
		for(i=0;i
Now the when I compile and run this, if I call the function once it works;twice and it doesn''t work but it does not cause the violateion;3 times and up it gives me the error; 

Share this post


Link to post
Share on other sites
The debugger, when it thinks something, is right

You need to check the results from fopen() to make sure it is a valid file handle, and memset to make sure it isn''t returning an error code. fopen() will cause access violations when files are missing, under incorrect permissions (NT, 2k, XP, 2k3 only), etc.

Share this post


Link to post
Share on other sites
I looked at it more closely with the debugger and all the values are what they should be right before the memset function. When it hits the memset it gives me the error.

[edited by - tHiSiSbOb on July 8, 2003 6:35:29 PM]

Share this post


Link to post
Share on other sites
You used

texts[numTexts]

which is out of bounds. You should have used

texts[numTexts-1]

Also, if you

new yada[]

then you should

delete [] yada


EDIT -- Fixed formatting

[edited by - Dave Hunt on July 8, 2003 8:32:24 PM]

Share this post


Link to post
Share on other sites