473,385 Members | 1,942 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,385 software developers and data experts.

Returning a wstring from a method...

Hi,

I have a very simple function, declared like this:

wstring CMyClass::GetDate()
{
....
wcsftime (sDateString,255,L"%d-%b-%Y",&dTmpDate);
wstring sText(sDateString);
return sText;
}

I call it like this:

wstring myString = GetDate();

Nothing exciting there, but whever I run the program in debug mode I
get an Access Violation Error - "Access violation reading location
0xfdfdfdfd.". The error happens on the return line. The program seems
to run fine when I don't debug into it :(

Does anyone know what this could be? I'm using Microsoft VS2005 as the
IDE.

Thanks

Aug 11 '06 #1
9 7415
Mwob wrote:
Hi,

I have a very simple function, declared like this:

wstring CMyClass::GetDate()
{
....
wcsftime (sDateString,255,L"%d-%b-%Y",&dTmpDate);
wstring sText(sDateString);
return sText;
}
That's unlikely to compile, with the dots and no defintion of
sDateString.
I call it like this:

wstring myString = GetDate();

Nothing exciting there, but whever I run the program in debug mode I
get an Access Violation Error - "Access violation reading location
0xfdfdfdfd.". The error happens on the return line.
Typical error pattern written to memory after deallocation of an
object.
And of course, the return line is when local objects are destroyed.
The program seems to run fine when I don't debug into it :(
Might be because the error pattern isn't written in release mode. The
goal of the error pattern is so the application crashes as early as
possible,
which is good in debugging. It only slows the release build of your
app.

HTH,
Michiel Salters

Aug 11 '06 #2

Mwob wrote:
Hi,

I have a very simple function, declared like this:

wstring CMyClass::GetDate()
{
....
wcsftime (sDateString,255,L"%d-%b-%Y",&dTmpDate);
wstring sText(sDateString);
return sText;
}

I call it like this:

wstring myString = GetDate();

Nothing exciting there, but whever I run the program in debug mode I
get an Access Violation Error - "Access violation reading location
0xfdfdfdfd.". The error happens on the return line. The program seems
to run fine when I don't debug into it :(
It could be a problem with something in a watch-window or something
like that. I've had a similar problem with Visual Studio once (not
2005, though): closing some of the debug-windows removed the problem.

/Peter
>
Does anyone know what this could be? I'm using Microsoft VS2005 as the
IDE.

Thanks
Aug 11 '06 #3
Thanks for the response.

I'm a bit rusty with C++, its been a while since I revisted this page.
The dots were just entered by me to remove unesessary code that wasn't
relevent.

I'm not sure what to do here. Am I going about returning a wstring the
wrong way? Shoudl I structure the code differently? ..... I'll include
that class definiton again with more info this time:

wstring CInterval::GetDate(DateType retdate)
{
// First I declate a local string to hold my formatted date.
wchar_t sDateString[12];

// I then process some business rules, which I will ommit from
here...

// I then use a method to popupate with the date:
wcsftime (sDateString,255,L"%d-%b-%Y",&dTmpDate);

// Because I want to return a wstring, I instantiate one and pass the
// wchat_t in as a constructor
wstring sText(sDateString);

// Finally, I return that new string
return sText; // Here is where the error occurs
}

Should I be creating my method differently?

Thanks again for your help!
BTW - I tried closing all local debugging windows, that didn't help.
Mi*************@tomtom.com wrote:
Mwob wrote:
Hi,

I have a very simple function, declared like this:

wstring CMyClass::GetDate()
{
....
wcsftime (sDateString,255,L"%d-%b-%Y",&dTmpDate);
wstring sText(sDateString);
return sText;
}

That's unlikely to compile, with the dots and no defintion of
sDateString.
I call it like this:

wstring myString = GetDate();

Nothing exciting there, but whever I run the program in debug mode I
get an Access Violation Error - "Access violation reading location
0xfdfdfdfd.". The error happens on the return line.

Typical error pattern written to memory after deallocation of an
object.
And of course, the return line is when local objects are destroyed.
The program seems to run fine when I don't debug into it :(

Might be because the error pattern isn't written in release mode. The
goal of the error pattern is so the application crashes as early as
possible,
which is good in debugging. It only slows the release build of your
app.

HTH,
Michiel Salters
Aug 11 '06 #4
"Mwob" <ma*********@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
: I'm not sure what to do here. Am I going about returning a wstring the
: wrong way? Shoudl I structure the code differently? ..... I'll include
: that class definiton again with more info this time:
:
: wstring CInterval::GetDate(DateType retdate)
: {
: // First I declate a local string to hold my formatted date.
: wchar_t sDateString[12];
:
: // I then process some business rules, which I will ommit from
: here...
:
: // I then use a method to popupate with the date:
: wcsftime (sDateString,255,L"%d-%b-%Y",&dTmpDate);
:
: // Because I want to return a wstring, I instantiate one and pass the
: // wchat_t in as a constructor
: wstring sText(sDateString);
:
: // Finally, I return that new string
: return sText; // Here is where the error occurs
This is all ok, although you could drop the intermediate
variable and directly write:
return sDateString;
: }
:
: Should I be creating my method differently?
Unless there is a bug in the implementation of wstring (very
unlikely), the only possible problem in the above code is
with the sDateString buffer: are 12 chars provably enough,
regarding of the locale? Try increasing the buffer size.

If this is not the culprit, then the problem is most likely
in the code fragments that you omitted from your post.
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Aug 11 '06 #5
Thanks.

Hmmm, I stripped the method right down so that it now only contains
this code (no ommitted code this time)

wstring CInterval::GetDate(DateType retdate)
{

wchar_t sDateString[100]; // Plenty of room in here
tm dTmpDate;
dTmpDate = dFrom; // This is a valid initialised "tm" structure.

wcsftime (sDateString,255,L"%d-%b-%Y",&dTmpDate);
wstring sText(sDateString);

return sText; // Still errors here!

}

This still falls over, even with a much bigger buffer (it shouldn't be
more that 12 chars in length) and all over code deleted from the
method!


Ivan Vecerina wrote:
"Mwob" <ma*********@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
: I'm not sure what to do here. Am I going about returning a wstring the
: wrong way? Shoudl I structure the code differently? ..... I'll include
: that class definiton again with more info this time:
:
: wstring CInterval::GetDate(DateType retdate)
: {
: // First I declate a local string to hold my formatted date.
: wchar_t sDateString[12];
:
: // I then process some business rules, which I will ommit from
: here...
:
: // I then use a method to popupate with the date:
: wcsftime (sDateString,255,L"%d-%b-%Y",&dTmpDate);
:
: // Because I want to return a wstring, I instantiate one and pass the
: // wchat_t in as a constructor
: wstring sText(sDateString);
:
: // Finally, I return that new string
: return sText; // Here is where the error occurs
This is all ok, although you could drop the intermediate
variable and directly write:
return sDateString;
: }
:
: Should I be creating my method differently?
Unless there is a bug in the implementation of wstring (very
unlikely), the only possible problem in the above code is
with the sDateString buffer: are 12 chars provably enough,
regarding of the locale? Try increasing the buffer size.

If this is not the culprit, then the problem is most likely
in the code fragments that you omitted from your post.
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Aug 11 '06 #6
"Mwob" <ma*********@gmail.comschrieb:
wchar_t sDateString[100]; // Plenty of room in here
wcsftime (sDateString,255,L"%d-%b-%Y",&dTmpDate);
The "255" should be "sizeof(sDateString)/sizeof(wchar_t)".

T.M.
Aug 11 '06 #7
Of course it shoudl!

And thats fixed the error too :) Thanks very much for pointing that out
to me :)
Torsten Mueller wrote:
"Mwob" <ma*********@gmail.comschrieb:
wchar_t sDateString[100]; // Plenty of room in here
wcsftime (sDateString,255,L"%d-%b-%Y",&dTmpDate);

The "255" should be "sizeof(sDateString)/sizeof(wchar_t)".

T.M.
Aug 11 '06 #8
Mwob wrote:
Of course it shoudl!
Please read the information below.


Brian
--
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
Aug 11 '06 #9
In article <u3***********@shared-files.de>, de******@shared-files.de
says...
"Mwob" <ma*********@gmail.comschrieb:
wchar_t sDateString[100]; // Plenty of room in here
wcsftime (sDateString,255,L"%d-%b-%Y",&dTmpDate);

The "255" should be "sizeof(sDateString)/sizeof(wchar_t)".
Better still: sizeof(sDateString)/sizeof(sDateString[0])

For example, assume that your compiler defines wchar_t as 16 bits, but
you decide you want to be able to hold UCS-4 characters, so you change
sDatrString into:

unsigned long sDateString[100];

Of course, something like:

typedef wchar_t char_type;

which you could then change to:

typedef unsigned long char_type;

and as long as everything else referred to char_type instead of wchar_t,
life would be good. Of course, that's supposed to be the idea of wchar_t
in the first place, but (unfortunately) many compilers define it as a
16-bit type, which can be problematic.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 13 '06 #10

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

Similar topics

1
by: Saurabh Aggrawal | last post by:
Hi, On line nos. 24, 25, 26 24: wstring cfMethods = {{L"setLabel"},{L""}}; 25: wstring cfProperties= {{L"isVisible"},{L""}}; 26: wstring cfEvents =...
6
by: gerg | last post by:
How would one convert a wstring to a string? This is what I have so far: bool copyto(std::string& l,std::wstring& r) { bool ret = false; size_t i = 0; const size_t n = r.length()+1;
3
by: uday.sen | last post by:
Hi, I am porting a piece of code from VC++ to linux platform. My compiler is g++ 3.2.2. I am getting following error: no matching function for call to A::setResponse(std::wstring) candidates...
8
by: Divick | last post by:
Hi all, can somebody tell how much std::wstring is supported across different compilers on different platforms? AFAIK std::string is supported by almost all C++ compilers and almost all platforms,...
2
by: wolverine | last post by:
Hi Could any one tell me how to compile the code using wstring with g++ or gcc. I am using gcc version 3.4.2 #include<iostream> #include<string> using namespace std; int main() {
10
by: v4vijayakumar | last post by:
1. why the following program is not working as expected? #include <iostream> using namespace std; int main() { string t("test"); wcout << (wchar_t *) t.c_str() << endl; wcout << t.c_str()...
1
by: v4vijayakumar | last post by:
1. Why the following program is not working as expected? #include <iostream> using namespace std; int main() { string t("test"); wcout << (wchar_t *) t.c_str() << endl; wcout << t.c_str()...
10
bajajv
by: bajajv | last post by:
Hi, I was trying to implement CString with std::string and std::wstring. class CString { public: std::string str; CString() {str = "ABCD";} //this works }: class CString {
3
bajajv
by: bajajv | last post by:
Hi, I want to implement the cstring class using wstring. but in assignement operator, I need to know how to add null while assigning some wchar_t type of data to the wstring. below is my code - ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.