3

some simple code has begun failing in an sdk I've been working with, also it apparently has been working correctly for a long time, and indeed I'm almost positive I have compiled parts of the code like this and they have worked, but recently have failed.

example with values confirmed in debugger:

void SomeFunction(CString& paramstring) //let's pretend we pass a string with the value "hello"
{
     int size=paramstring.GetLength(); //size now contains an incorrect shorter value like 4
     CString localstring=paramstring;//localstring contains something like "hell" or "hel"
}

any ideas why this might be happening

FailedDev
  • 26,680
  • 9
  • 53
  • 73
  • A hint for you: when you paste code, highlight it all and hit the `{}` button to format it properly. – Mark Ransom Nov 10 '11 at 23:02
  • 7
    There must be some information that you're not disclosing in this short sample, because there's absolutely no reason for it to fail. – Mark Ransom Nov 10 '11 at 23:04
  • What are you doing with the CString? Are you modifying it in any way? How does it look like before you pass it to the function? – FailedDev Nov 10 '11 at 23:13
  • What's this CString type anyway? – zwol Nov 10 '11 at 23:13
  • 1
    @Zack I think it's M$oft ATL class. – FailedDev Nov 10 '11 at 23:15
  • 5
    It's MFC. And using M$ is petty, at best. – Puppy Nov 10 '11 at 23:22
  • MFC also has a pretty large notice that you shouldn't pass them between modules, unless your code is built with a few other settings. Anything like that going on? – ssube Nov 10 '11 at 23:38
  • @DeadMG : MFC uses it, but it's in namespace `ATL`; I'd say it's ATL. ;-] – ildjarn Nov 11 '11 at 00:31
  • unfortunately I don't have access to the source code where the CString is being passed. I'm writing a plugin using an sdk. I've done a few tests. First in the debugger all the characters in paramstring are there. it just doesn't copy them all on assignment. Also I realized it always truncates to 3 characters. if it's 3 characters or less, it works as expected. – nsfnotthrowingaway Nov 11 '11 at 16:47

3 Answers3

2

ive tried your code in a console application and it works perfectly the problem comes from another part of your code here is the the code try it out (use MFC )

#include <iostream>
#include <afx.h>
using namespace std;

//let's pretend we pass a string with the value "hello"
void fun(CString& paramstring)
{
    int size=paramstring.GetLength();  
    cout<<"size= "<<size<<"\n";
    CString localstring=paramstring;
    wcout<<"string = "<<(LPCTSTR)localstring<<"\n";
}

int main()
{
    CString s ("Hello");
    fun(s);
}

output:

size = 5 string = Hello

ildjarn
  • 62,044
  • 9
  • 127
  • 211
andrewmag
  • 252
  • 1
  • 4
  • 11
1

My only guess is you have a memory overwrite corrupting that object.

Time to bring out the power tools. Install either Purify or Boundschecker and hunt your bug down.

marinara
  • 538
  • 1
  • 5
  • 10
  • thank you. unfortunately, I don't have access to that part of the source code. I'm using a plugin sdk, and this is a function I'm to overwrite. – nsfnotthrowingaway Nov 11 '11 at 15:49
0

I have made a mistake in CString code in the past which stopped CStrings working correctly when passed as parameters. Your problem might be completely different, but I will explain here what I did to cause the problem, as it might help someone.

My CString contained a directory name. In order to clean it up a little, I checked if the last character was '\', and if so I replaced it with '\0' by directly overwriting the character inside the CString.

That probably somehow destroyed the integrity of the CString by creating a mismatch between the length of the string as measured by things like strlen(), and the actual size of the buffer. My explanation of the problem might be not be correct, but it basically boiled down to me corrupting the integrity of the CString, I assume.

I fixed my problem by replacing my statement with something like dirname=dirname.Left(dirname.GetLength()-1);

I am writing this from memory and don't use the functions often, so forgive me if I have remembered a function name incorreclty. Anyway, my recommendation is not to write null characters into the middle of a CString to shorten it, and to look for that (among other things) if a problem occurs with CStrings as parameters.

Ah yes, I think the problem might be the reverse of yours. If you shorten a string by overwriting a character then maybe GetLength() will return the old value, which is larger than than you might expect.

My actual problem was that dir + "\" + filename was not working. I suppose it was concatentating strings containing a null and that the final value was just the directory part, as that is where the null had been added.

Ivan
  • 4,383
  • 36
  • 27