473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Error returning a string

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?

Aug 25 '06 #1
11 1613
solarin wrote:
Hi,

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

HTH,
Michiel Salters

Aug 25 '06 #2

Mi*************@tomtom.com ha escrito:
>
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.

Aug 25 '06 #3

solarin wrote:
Mi*************@tomtom.com ha escrito:

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

Aug 25 '06 #4

peter koch ha escrito:

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,

Aug 25 '06 #5
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.

Aug 25 '06 #6

solarin wrote:
peter koch ha escrito:

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

Aug 25 '06 #7
solarin wrote:
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();
}

>
}

//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
Aug 25 '06 #8

peter koch ha escrito:
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,

Aug 25 '06 #9

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

peter koch ha escrito:
>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
Aug 25 '06 #10

Bo Persson ha escrito:
"solarin" <en********@gmail.comskrev i meddelandet
news:11**********************@75g2000cwc.googlegro ups.com...

peter koch ha escrito:
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.

Aug 25 '06 #11
solarin schrieb:
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
Aug 26 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gizmo | last post by:
hello all have been trying to write a Mid() function a bit like the one in vb. i come to compile it and there are no errors however when i run it an error accours and it says the program has to...
3
by: Baloff | last post by:
Hello this code is putting our errors which are diffecult for me to fix, I have been thinking about it for a while now... thanks for helping ...
4
by: jt | last post by:
I'm getting a compiler error: warning C4172: returning address of local variable or temporary Here is the function that I have giving this error: I'm returning a temporary char string and its...
0
by: John Constant | last post by:
Using the MS C++ Example http://support.microsoft.com/default.aspx?scid=kb;en-us;261003 I've successfully managed to trap and log Script Errors that are generated by the WebBrowser (IE 6) which is...
9
by: L | last post by:
Hi guys, I am writing some interop code to print graphics to an eps file and I use adobe printer driver. The piece of code I have written is working fine. but if I loop for more than 200 times,...
1
by: John Chorlton | last post by:
I've been attempting to pass a chunk of data back from a child Windows form using public properties on the form and have been getting some odd errors. I wanted to return a row of data to avoid...
8
by: ST | last post by:
Hello everyone, Can anyone help me with this error above when I debug my web app project in vstudio.net?? I can't figure it out! It was working fine for months, and now all of a sudden it's not!!...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
13
by: lithoman | last post by:
I'm stumped here. I run the procedure Batch_Select against the database with @ID=18 and I get the expected data. When it loads into a SqlDataReader, it gets messed up somehow. Initially, after the...
4
by: nick048 | last post by:
Hi In the main I have declared a variable char string; I need to construct this variable with a function and I have written the code: char stringReceived(int sockDesc){ char unsigned c;...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.