Garbage with pointers in a class, C++


I am using Borland Builder C++. I have a memory leak and I know it must be because of this class I created, but I am not sure how to fix it. Please look at my code-- any ideas would be greatly appreciated!

Here's the .h file:

#ifndef HeaderH
#define HeaderH
#include <vcl.h>
#include <string>
using std::string;
class Header {

    //File Header
    char FileTitle[31];
    char OriginatorName[16];

    //Image Header
    char ImageDateTime[15];
    char ImageCordsRep[2];
    char ImageGeoLocation[61];

    NitfHeader(double latitude, double longitude, double altitude, double heading);
    void SetHeader(char * date, char * time, double location[4][2]);  


    void ConvertToDegMinSec (double angle, AnsiString & s, bool IsLongitude);
    AnsiString ImageDate;
    AnsiString ImageTime;
    AnsiString Latitude_d;
    AnsiString Longitude_d;
    double Latitude;
    double Longitude;
    double Heading;
    double Altitude;


And here is some of the .cpp file:

void Header::SetHeader(char * date, char * time, double location[4][2]){
    //File Header
strcpy(FileTitle,"Cannon Powershot A640");
strcpy(OperatorName,"Camera Operator");

   //Image Header
//Image Date and Time
   ImageDate = AnsiString(date);
   ImageTime = AnsiString(time);
   AnsiString secstr = AnsiString(ImageTime.SubString(7,2));
   AnsiString rounder = AnsiString(ImageDate.SubString(10,1));
   int seconds = secstr.ToInt();
    //Round off seconds  - will this be necessary with format hh:mm:ss in text file?
   if (rounder.ToInt() > 4) {
   AnsiString dateTime = ImageDate.SubString(7,4)+ ImageDate.SubString(4,2) + ImageDate.SubString(1,2) + ImageTime.SubString(1,2)
					+ ImageTime.SubString(4,2) + AnsiString(seconds);

   //Image Coordinates Representation

   //Image Geographic Location
   AnsiString lat;
   AnsiString lon;
   AnsiString locationlat_d;
   AnsiString locationlon_d;
   AnsiString corner;

   for (int i = 0; i < 4; i++){


     if(location[i][0] < 0){
	    locationlat_d = 'S';
      }else if(location[i][0] > 0){
	    locationlat_d = 'N';
     }else locationlat_d = ' ';

     if(location[i][1] < 0){
	    locationlon_d = 'W';
     }else if(location[i][1] > 0){
	     locationlon_d = 'E';
     }else locationlon_d = ' ';

     corner += lat + locationlat_d + lon + locationlon_d;



Now when I use the class in main, basically I just create a pointer:

Header * header = new Header;
char * imageLocation = header->ImageGeoLocation;
//do something with imageLocation
delete header;

Where corners->location is a string from another class, and t[5] and t[6] are both strings. The problem is that imageLocation doesn't contain what is expected, and often just garbage. I have read a lot about memory leaks and pointers, but I am still very new to programming and some of it is quite confusing. Any suggestions would be fabulous!!

By : Shishiree


So I changed strcpy() to strncpy() and it got rid of the garbage! Thank you everyone!

By : Shishiree

Thank you, Torlack, and others for replying so quickly. Basically, imageLocation gets populated fine, unless I have other code before it. For example, I have this string list, which basically contains file names.

    AnsiString fileType ("*.jpg");
	AnsiString path = f + fileType;
	HANDLE hFindJpg = FindFirstFile(path.c_str(),&fd);

   //Find all images in folder
	TStringList * imageNames = new TStringList;

	if (hFindJpg != INVALID_HANDLE_VALUE) {

			if(!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)){
				image = AnsiString(fd.cFileName);


	}else ShowMessage ("Cannot find images.");


Now when I try to refer to an image from the list directly before, I get the name of the image put inside imageLocation.

 //char * imageLocation = header->ImageGeoLocation; //as expected
char * imageLocation = header->ImageGeoLocation; //puts name of jpg file in imageLocation
By : Shishiree

Is your problem that ImageGeoLocation is trash or you have a memory leak?

If you code is written as such:

Header * header = new Header;
char * imageLocation = header->ImageGeoLocation;
delete header;
printf("ImageLocation is %s", imageLocation);

Then you problem isn't a memory leak, but that you are deleting the memory out from under imageLocation. ImageLocation is just a pointer and doesn't actually contain data, it just points to it. So if you delete the data, then the pointer is pointing to trash.

If that isn't the case, then debug your SetHeader method. Is ImageGeoLocation getting populated with data as you expect? If it is, then imageLocation must point to valid data unless there is some omitted code that is damaging ImageGeoLocation later on. A memory what window looking at ImageGeoLocation can help since you will be able to step through your code and see which line actually changes ImageGeoLocation where you don't expect.

By : Torlack

This video can help you solving your question :)
By: admin