string x = argv[1]: <Bad Ptr>

Hi all,

I have looked everywhere (on these forums and elsewhere) but can't find a solution to my problem.
I have the following code:

1
2
3
4
5
6
7
8
9
10
11
12
int _tmain(int argc, _TCHAR *argv[])
{
	// if no arguments are given, print a message and exit
	if(argc < 2)
	{
		printf("Please provide a path to the input images.");
		return 0;
	}
		
	// put the input_path parameter in a string variable.
	string in_path = argv[1]; // argv[0] is the call to this program.
}


There is some more obviously after getting the in_path, but this is where it goes awry.

When I do a debug build, my in_path is set. But, when I do a release build, I get a <Bad Ptr>!
I have checked the debug call and the debug call for the deployment build also adds the correct parameter. I don't understand why this is only happening with my release build. I have compared the two build configurations and cannot find any relevant differences, other than the obvious because of optimisations.
Character set is "Use Multi-Byte Character Set" for both configurations.

Any help would be appreciated!
Last edited on
Are you sure the error occurs on line 11? (And not some line below it)

What is the full error message?
Last edited on
Since you are using _tmain and TCHAR's you should not be assuming you can just use std::string. You could need std::wstring for all we know. You should do a typedef:

typedef std::basic_string<_TCHAR> tstring;

And use tstring instead of string. Not saying that this is the cause of the problem, though.

I would also do if (argc != 2) if you are expecting one and exactly one argument always.
I don't exactly understand what you want, but debugging this might make the problem more obvious.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <vector>
#include <string>

int main(int argc, char *argv[])
{
	std::vector < std::string > strings;
	if(argc < 2)
	{
		printf("Please provide a path to the input images.");
	}
	else
	{
		for(int i=0; i<argc; ++i)
		{
			strings.push_back(argv[i]);
		}
	}

	return 0;
}
Okay, thanks everyone for your help, but it turns out that I was on a false trail here ...

The call to OpenCV's cv::imread seems to be buggy in a deployment build. I pass it the path to the image and it breaks. So I started debugging the deployment build and my debugger says the string is a <Bad Ptr>.
Turns out, that's just because you can't properly debug optimised code (hence the debug build, I guess).
Further investigation (using printf to see the string through code, rather than the debugger) showed that the string was correct. It was the call to imread that has problems, accessing invalid memory addresses.

I have posted this problem on the OpenCV forums now.

Sorry about this, seems I still have a lot to learn about C++ ...
Topic archived. No new replies allowed.