_CrtCheckMemory() error!?

This is a discussion on _CrtCheckMemory() error!? within the C++ Programming forums, part of the General Programming Boards category; Hello, I am trying to execute my program but I keep receiving a _CrtCheckMemory() error message. I have attached an ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    59

    Memory error!?

    Hello,

    I am trying to execute my program but I keep receiving a _CrtCheckMemory() error message.

    I have attached an image screenshot of my error message box.

    I have about 6 classes with several character pointers referencing dynamic memory. I don't think memory should be a problem since I have 512 MB of memory.

    The error happens in a consistant place in each execution.

    What can be causing this error?

    What can I do to stop it?
    Attached Images Attached Images  
    Last edited by VanJay011379; 07-31-2002 at 12:59 PM.
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    59
    I've narrowed it down to one of the following lines in my code where the error pops up.

    Code:
    void Polygon::setBoundarycaptype(char * bctArg)
    {
    	cout << "Begin setBoundarycaptype" << endl; //debugging
    	cout << strlen(bctArg) << endl;	//debugging
    	boundarycaptype = new char[strlen(bctArg) + 1];
    	cout << "Dynamic memory assigned" << endl; //debugging
    	strcpy(boundarycaptype, bctArg);
    	cout << "End setBoundarycaptype" << endl; //debugging
    }
    Please disregard my debugging cout statments.

    In particular, the following code line seems to be where the error pops up:
    Code:
    boundarycaptype = new char[strlen(bctArg) + 1];
    boundarycaptype is of type char * in the Polygon class.

    Why does the above line give the error message?

    If it helps here is the defintion of my Polygon class:

    Code:
    #ifndef POLYGON_H 	//conditional compilation if the county class is not defined
    #define POLYGON_H	//define the Polygon class
    
    #include "Layer.h"	//include the layer base class
    
    //begin the Polygon class
    class Polygon: public Layer
    {
    	//declare private data members
    	private:
    
    		float boundarytransparency;	//transparency of the boundary
    		float filltransparency;		//transparency of the inside
    		char * boundarycaptype;		//boundary capture limits
    		char * fillcolor;			//color
    		
    	//declare the public data members
    	public:
    
    		//constructor
    		Polygon(char *, const char *, char *, char *);		//constructor
    		
    		//get 
    		const float getBoundarytransparency();
    		const float getFilltransparency();
    		const char * getFillcolor();
    		const char * getBoundarycaptype();
    
    		//set
    		void setBoundarytransparency(float);
    		void setFilltransparency(float);
    		void setFillcolor(char *);
    		void setBoundarycaptype(char *);
    
    		//virtual functions with definitions in Polygon class
    		virtual void setLayerType();			//featureclass or image
    		virtual void setLayerName(char *);		//name for map legend layer on map						
    		virtual void setVisible();				//true or false
    		virtual void setDataSetName(char *);	//the dataset name (faunivs_0)
    		virtual void setDataSetType();			//Polygon, line, polygon or image
    		virtual void setWorkSpaceName(const char *);	//the dataset workspace name (munic)
    
    		virtual void printLayer();	//prints the layer
    		        
    };	//end the Polygon class	
    
    #endif POLYGON_H		//conditional compilation end if the Polygon class is not defined
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You may need to free memory before you allocate boundarycaptype again. Initialize boundarycaptype to NULL. Then:
    Code:
    void Polygon::setBoundarycaptype(char * bctArg)
    {
    	cout << "Begin setBoundarycaptype" << endl; //debugging
    	cout << strlen(bctArg) << endl;	//debugging
    	if boundarycaptype != NULL
    	   delete []boundarycaptype;
    	boundarycaptype = new char[strlen(bctArg) + 1];
    	cout << "Dynamic memory assigned" << endl; //debugging
    	strcpy(boundarycaptype, bctArg);
    	cout << "End setBoundarycaptype" << endl; //debugging
    }

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Probably you corrupted your memory somewhere else.
    Make sure when you use new [] you use delete[].

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    59
    Originally posted by Nick
    Probably you corrupted your memory somewhere else.
    Make sure when you use new [] you use delete[].
    Actually, I have been running a lot of tests without deleting my dynamic memory.

    Is there any way reclaim memory that is lost from a previous run?

    I have tried resetting my machine but that doesn't seem to work.
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    59
    Originally posted by swoopy
    You may need to free memory before you allocate boundarycaptype again. Initialize boundarycaptype to NULL. Then:
    Code:
    void Polygon::setBoundarycaptype(char * bctArg)
    {
    	cout << "Begin setBoundarycaptype" << endl; //debugging
    	cout << strlen(bctArg) << endl;	//debugging
    	if boundarycaptype != NULL
    	   delete []boundarycaptype;
    	boundarycaptype = new char[strlen(bctArg) + 1];
    	cout << "Dynamic memory assigned" << endl; //debugging
    	strcpy(boundarycaptype, bctArg);
    	cout << "End setBoundarycaptype" << endl; //debugging
    }
    That delete line gives me a 'System.NullReferenceException' message saying
    "Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object."
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    59
    The odd thing about my first posted code is it works for about 107 objects then the error appears.
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21