Connecting Tech Pros Worldwide Forums | Help | Site Map

Error returning a string

solarin
Guest
 
Posts: n/a
#1: Aug 25 '06
Hi,

I've the following code:

//all variables are defined in the code

Class CMessage{

string CMessage::GenerateString()
{

string finalmessage;
stringstream s;
s << codeClass;
finalmessage = "CODE: "+s.str() + " - ";
//finalmessage = codeClass + "----";
finalmessage = finalmessage + messageClass + " - ";

return finalmessage;
}

}

//A.h

Class A {
public:
string StringToLogger(MessagesNotifier_Client* client, CMessage
*msg)
private:
string msgToLogger;
}


// A.cpp

string A::StringToLogger(MessagesNotifier_Client* client,CMessage
*msg)
{
msgToLogger = msg->GenerateString(); // Error here.
invokerClass = typeid( client ).name();
msgToLogger = msgToLogger + invokerClass;
msgToLogger = msgToLogger + "----";
// Enviamos: "Codigo_de_error" + ---- + "miss de error" + ---- +
"Nombre de la classe"
return msgToLogger;

}

}


Inside de class A I'm calling the method "GenerateString()" from class
CMessage. This method must return a string. The method is generates
the string without problems. The error is when the result from
"msg->GenerateString()" must be placed in msgToLogger.

the Error is:
Access violation reading in 0xcdcdcdf1.
Access violation writting in 0xcdcdcdcd.


Any ideas or hints to solve it?


Michiel.Salters@tomtom.com
Guest
 
Posts: n/a
#2: Aug 25 '06

re: Error returning a string


solarin wrote:
Quote:
Hi,
>
I've the following code:
[snip]
Quote:
string A::StringToLogger(MessagesNotifier_Client* client,CMessage
*msg)
{
msgToLogger = msg->GenerateString(); // Error here.
Quote:
the Error is:
Access violation reading in 0xcdcdcdf1.
You need to pass a CMessage object.

HTH,
Michiel Salters

solarin
Guest
 
Posts: n/a
#3: Aug 25 '06

re: Error returning a string



Michiel.Salters@tomtom.com ha escrito:
Quote:
>
You need to pass a CMessage object.
>
What you mean? I'm receiving a CMessage object in the method
StringToLogger (where the execution fails) and i'm using it. Then , I
use the CMessage object to use the method (declared in CMessage)
GenerateString.

peter koch
Guest
 
Posts: n/a
#4: Aug 25 '06

re: Error returning a string



solarin wrote:
Quote:
Michiel.Salters@tomtom.com ha escrito:
>
Quote:

You need to pass a CMessage object.
>
What you mean? I'm receiving a CMessage object in the method
StringToLogger (where the execution fails) and i'm using it. Then , I
use the CMessage object to use the method (declared in CMessage)
GenerateString.
No -you are not receiving a CMessage object: what you receive is an
invalid pointer to a CMessage object (perhaps the object was deleted at
some point). At least this is how i interpret michiels post - and i
believe he is correct.

/Peter

solarin
Guest
 
Posts: n/a
#5: Aug 25 '06

re: Error returning a string



peter koch ha escrito:

Quote:
No -you are not receiving a CMessage object: what you receive is an
invalid pointer to a CMessage object (perhaps the object was deleted at
some point). At least this is how i interpret michiels post - and i
believe he is correct.

But the error does not happend when I try to do msg->GenerateString().
I've debugged that and it's ok. if "msg" is an invalid pointer, could I
debug "msg->GenerateString". Once the string is generated (in
GenerateString), the error happend when I try to do:

msgToLogger = "result of the method GenrateString inside CMessage" ,
after GenerateString has generate a string.

Regards,

solarin
Guest
 
Posts: n/a
#6: Aug 25 '06

re: Error returning a string


I can avoid the error doing the following:

// A.cpp

string A::StringToLogger(MessagesNotifier_Client* client,CMessage
*msg)
{
string msgToLogger,invokerClass; // New line added
msgToLogger = msg->GenerateString(); // Error here.
invokerClass = typeid( client ).name();
msgToLogger = msgToLogger + invokerClass;
msgToLogger = msgToLogger + "----";
// Enviamos: "Codigo_de_error" + ---- + "miss de error" + ----
+
"Nombre de la classe"
return msgToLogger;

}

Išve declared the msgToLogger and invokerClass strings inside the
method. But Išve these strings declared in the file A.h. Why I need to
declare this strings again?

Thanks for the support.

peter koch
Guest
 
Posts: n/a
#7: Aug 25 '06

re: Error returning a string



solarin wrote:
Quote:
peter koch ha escrito:
>
>
Quote:
No -you are not receiving a CMessage object: what you receive is an
invalid pointer to a CMessage object (perhaps the object was deleted at
some point). At least this is how i interpret michiels post - and i
believe he is correct.
>
>
But the error does not happend when I try to do msg->GenerateString().
I've debugged that and it's ok. if "msg" is an invalid pointer, could I
debug "msg->GenerateString". Once the string is generated (in
GenerateString), the error happend when I try to do:
>
msgToLogger = "result of the method GenrateString inside CMessage" ,
after GenerateString has generate a string.
>
Regards,
If the error also occurs when calling
msgToLogger = std::string("result of the method GenrateString inside
CMessage");
your original post was misleading. In this case the error is likely
inside the msgToLogger class.

/Peter

Kai-Uwe Bux
Guest
 
Posts: n/a
#8: Aug 25 '06

re: Error returning a string


solarin wrote:
Quote:
Hi,
>
I've the following code:
>
//all variables are defined in the code
>
Class CMessage{
>
string CMessage::GenerateString()
{
>
string finalmessage;
stringstream s;
s << codeClass;
finalmessage = "CODE: "+s.str() + " - ";
//finalmessage = codeClass + "----";
finalmessage = finalmessage + messageClass + " - ";
>
return finalmessage;
}
What about:

string CMessage::GenerateString()
{
stringstream s;
s << "CODE: "
<< codeClass
<< " - "
<< messageClass
<< " - ";
return s.str();
}

Quote:
>
}
>
//A.h
>
Class A {
public:
string StringToLogger(MessagesNotifier_Client* client, CMessage
*msg)
private:
string msgToLogger;
}
>
>
// A.cpp
>
string A::StringToLogger(MessagesNotifier_Client* client,CMessage
*msg)
{
msgToLogger = msg->GenerateString(); // Error here.
invokerClass = typeid( client ).name();
msgToLogger = msgToLogger + invokerClass;
msgToLogger = msgToLogger + "----";
// Enviamos: "Codigo_de_error" + ---- + "miss de error" + ---- +
"Nombre de la classe"
return msgToLogger;
>
}
>
}
>
>
Inside de class A I'm calling the method "GenerateString()" from class
CMessage. This method must return a string. The method is generates
the string without problems. The error is when the result from
"msg->GenerateString()" must be placed in msgToLogger.
>
the Error is:
Access violation reading in 0xcdcdcdf1.
Access violation writting in 0xcdcdcdcd.
>
>
Any ideas or hints to solve it?
Post enough code for us to reproduce the problem. Very likely you invoke
undefined behavior somewhere else.


Best

Kai-Uwe Bux
solarin
Guest
 
Posts: n/a
#9: Aug 25 '06

re: Error returning a string



peter koch ha escrito:
Quote:
your original post was misleading. In this case the error is likely
inside the msgToLogger class.
>
/Peter
Ok, my fault. Let me start again.

In class A, Išve declared a string in A.h (myString). The class A, has
a method that call another class and a string is returned.

A::StringToLogger(CMessage *msg)
{
myString = msg->GenerateString();
}

This code fails when the program tries to put the result of
"msg->GenerateString()" into myString. (myString is declared in A.h)

but if I do the following,

A::StringToLogger(CMessage *msg)
{
string myString // new declaration of myString.
myString = msg->GenerateString();
}

It runs ok.

The question is: Why I need to declare the string again?


Regards,

Bo Persson
Guest
 
Posts: n/a
#10: Aug 25 '06

re: Error returning a string



"solarin" <en.navarro@gmail.comskrev i meddelandet
news:1156504936.345976.129040@75g2000cwc.googlegro ups.com...

peter koch ha escrito:
Quote:
Quote:
>your original post was misleading. In this case the error is likely
>inside the msgToLogger class.
>>
>/Peter
>
>Ok, my fault. Let me start again.
>
>In class A, Išve declared a string in A.h (myString). The class A,
>has
>a method that call another class and a string is returned.
>
>A::StringToLogger(CMessage *msg)
>{
myString = msg->GenerateString();
>}
>
>This code fails when the program tries to put the result of
>"msg->GenerateString()" into myString. (myString is declared in A.h)
>
>but if I do the following,
>
>A::StringToLogger(CMessage *msg)
>{
string myString // new declaration of myString.
myString = msg->GenerateString();
>}
>
>It runs ok.
>
>The question is: Why I need to declare the string again?
You don't declare the string again, this is an entirely new string.
Unfortunately it is destroyed again at the end of the function. It
indicates that the msg parameter might be ok, though.

So the next question is: What does the declaration of the real
myString look like? Is it a std::string, or a CString, or a char*? Is
it static?

Is it perhaps damaged somewhere else in the code?


Bo Persson


solarin
Guest
 
Posts: n/a
#11: Aug 25 '06

re: Error returning a string



Bo Persson ha escrito:
Quote:
"solarin" <en.navarro@gmail.comskrev i meddelandet
news:1156504936.345976.129040@75g2000cwc.googlegro ups.com...
>
peter koch ha escrito:
>
Quote:
Quote:
your original post was misleading. In this case the error is likely
inside the msgToLogger class.
>
/Peter
Ok, my fault. Let me start again.

In class A, Išve declared a string in A.h (myString). The class A,
has
a method that call another class and a string is returned.

A::StringToLogger(CMessage *msg)
{
myString = msg->GenerateString();
}

This code fails when the program tries to put the result of
"msg->GenerateString()" into myString. (myString is declared in A.h)

but if I do the following,

A::StringToLogger(CMessage *msg)
{
string myString // new declaration of myString.
myString = msg->GenerateString();
}

It runs ok.

The question is: Why I need to declare the string again?
>
You don't declare the string again, this is an entirely new string.
Unfortunately it is destroyed again at the end of the function. It
indicates that the msg parameter might be ok, though.
>
So the next question is: What does the declaration of the real
myString look like? Is it a std::string, or a CString, or a char*? Is
it static?
>
Is it perhaps damaged somewhere else in the code?
>

Bo Persson


Its declared as std::string in A.h, and no is static.

Thomas J. Gritzan
Guest
 
Posts: n/a
#12: Aug 26 '06

re: Error returning a string


solarin schrieb:
Quote:
In class A, Išve declared a string in A.h (myString). The class A, has
a method that call another class and a string is returned.
>
A::StringToLogger(CMessage *msg)
{
myString = msg->GenerateString();
}
>
This code fails when the program tries to put the result of
"msg->GenerateString()" into myString. (myString is declared in A.h)
>
but if I do the following,
>
A::StringToLogger(CMessage *msg)
{
string myString // new declaration of myString.
myString = msg->GenerateString();
}
>
It runs ok.
>
The question is: Why I need to declare the string again?
You don't declare it again, you define another string.

The first function accesses a member function. So I would guess, that your
this-pointer is corrupt because you call the StringToLogger function with
an incorrect pointer to class A.

The real problem is in parts of the code that you didn't show us.

--
Thomas
Closed Thread