The problem seems pretty simple but I haven't figured it out yet.

I'm using wxWidgets to make a gui and this function gets called when I press enter while in a text field.

void Camera_Settings::on_exposure(wxCommandEvent &event)
{
	wxString temp = exposure->GetValue();
	char *value = (char *)temp.mb_str();
	char *hex_string = new char[3];
	itoa(atoi(value), hex_string, 16);

	string final = "ly3";
	final += hex_string;
	final += "\r";
	camera->command((char *)final.c_str());
}

For anyone unfamiliar with wxWidgets I'll just say that wxString is a string and (char *)temp.mb_str() gets the c string it represents.

When I run the program and get the message that I have a memory leak, I pretty sure it has to do with the hex_string since I have a leak of 3 bytes for each time I call this function. But if I simply call delete []hex_string at the end of the function my program crashes and says that it tried to write to the end of the heap.

Recommended Answers

All 8 Replies

Strange the following is working: final += hex_string; As far as I know hex_string is a pointer and final is a string, but I don't know whether the + operator is overloaded for data of type char* ...

I can do that but when I do this I'm told that I can add pointers

final = hex_string + "\r";

Strange the following is working: final += hex_string; As far as I know hex_string is a pointer and final is a string, but I don't know whether the + operator is overloaded for data of type char* ...

Of course, it's impossible to overload operator + for char* type ;)
It's overloaded for std::string:

std::string std::string::operator+(const char*) const;

;)

commented: Now I'm sure :) +2

since I don't say what the camera->command(char* command) I'll just say that I commented out the line and it seems to have to effect on the program. I also took out the string and did everything with char pointers, no improvement.

void Camera_Settings::on_exposure(wxCommandEvent &event)
{
	wxString temp = exposure->GetValue();
	char *value = (char*)temp.mb_str();
	char *hex_string = (char*)malloc(3);
	itoa(atoi(value), hex_string, 16);

	char *final = (char*)malloc(7);
	strcpy(final, "ly3");
	strcat(final, hex_string);
	strcat(final, "\r");
	//camera->command(final);
	free(final);
	free(hex_string);
}

What's a strange code... You overwrite memory if the temp string represents a value greater than 255 or less than 0. Keep it simpler:

char hex[24]; // or what else but not 3 bytes
itoa(atoi(temp.mb_str()),hex,16); // senseless for negatives
... // as in OP

That's all, no need in delete anything, no buffer overwriting...

According to your code you want to convert a value (integer) to a hexadecimal number, why don't you just declare value as a string and use stringstreams to convert the number to a hexadecimal number like here: http://www.daniweb.com/code/snippet1162.html :) ...

One obvious reason for the error is that 'value' contains a string which, when converted to hex, generates more than 2 digits and a zero terminator. In this case it would exceed your 3 character allocation.
I presume you've checked that this isn't happening?

I seem to be pretty incompetent with string in c++, anyway I've settled on this code.

void Camera_Settings::on_exposure(wxCommandEvent &event)
{
	wxString temp = exposure->GetValue();
	stringstream ss;
	string s;
	if(atoi((char*)temp.mb_str()) > 4095)
	{
		return;
	}
	ss << hex << atoi((char*)temp.mb_str());
	ss >> s;

	string final = "ly3";
	final += s;
	final += "\r";
	camera->command((char *)final.c_str());
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.