Skip to main content
gamedev.net
🔒 Locked

std::string problem.

Started by limpacp 5 replies 3.1k views
Original Post
limpacp
limpacp
Hello. Some times, after starting my app, i found that some other apps (f.e. photoshop) stops working propertly. I've checked my app trough DevPartner and it found many erros and memory leaks. Most of the errors i've already fixed, but i can't resolve the next situation: i have LPSTR szString that was readed from XML. Then i need to convert it to std::string variable. So it looks like this: std::string strNodeName = szNodeName; The compiller is happy with that, but DevPartner says: "Unrelated Pointer: Comparing two unrelated pointers 0x02402F48 and 0x0012FAA4. Pointers are not within the same buffer." And when my app is terminated, i've got the Memory Leak in xtring line 44. I think there is some relationship between this two issues. Actually i need a clear way to covert LPSTR (char *) to std::string. Thanks!
Programmer16
Programmer16
Edit: Let me rephrase that. There is no special way to convert a char-pointer string to a std::string. Your assignment operation (std::string strNodeName = szNodeName; ) is perfectly fine.

Some more info might be helpful. For example, are you using a certain library for loading the XML file? If so, does it require you to release it/clean up somehow?
limpacp
limpacp
I'm using tinyXml.
Actually this errors happens not only in xml parser. DevPartner always shows this error when i'm trying to convert char * to std::string by =.
Endurion
Endurion
I've got that warning as well. It seems alright to me, although i don't like having warnings around.

std::string seems to check, if the passed pointer is maybe inside its own buffer. This triggers the warning that two unrelated pointers are being compared (well they are). This might be a problem in other cases but in this case it's only a sanity check.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
BloodWarrior
BloodWarrior
Quote:
Original post by Programmer16
Edit: Let me rephrase that. There is no special way to convert a char-pointer string to a std::string. Your assignment operation (std::string strNodeName = szNodeName; ) is perfectly fine.

Some more info might be helpful. For example, are you using a certain library for loading the XML file? If so, does it require you to release it/clean up somehow?


First I apologise cause at work we mainly do C although I am currently pushing development to C++. Therefore I hardly have any experience with std::string

But at home I am writing some stuff for myself and to use a string from char_t (I need it so I can convert a wxString to string) I tend to do:

std::string newstring(char_pointer);

this seems to work fine and running in debug is not showing up any mem leaks when I close the application. Maybe thats the problem? I never tried simply assigning it straight away.

By the way, limpacp, you say you have mem leaks... that szNodeName refers to a char[] right? not a pointer that was allocated somewhere and never freed?


Hope it helps.
Pain is Inevitable, Suffering is Optional.Unknown
Aardvajk
Aardvajk
std::string newstring(char_ptr);

is equivalent to

std::string newstring=char_ptr;

since the second one just translates to a call to the constructor

string::string(const char *s);
TheAdmiral
TheAdmiral
I imagine that your problem either lies elsewhere or that the consequences of this one go deep. Protected mode apps have no direct way of interfering with one another. Unless you are leaking your system out of pageable memory or are using a common interface that has its own system-wide resource management, there should be no way (outside of the absurd, such as WriteProcessMemory()) that your app can cause Photoshop to fail.

Also, I think DevPartner is perhaps just being a little too thorough. That pointer comparison looks valid to me. Granted, the sources are nowhere near each other, but 0x0012FAA4 is bang in the middle of your stack and 0x02402F48 looks like it's way up in himem, probably part of the global heap (or possibly in a DLL .xdata section). This suggests to me that the STL is simply performing a sanity check, to prevent unnecessary string copies, or perhaps a bounds check. I couldn't say for sure without seeing the STL code responsible, but I think you should look elsewhere for the source of your memory leaks.

Regards
Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.