I'm having a problem where I give a const char* a value and then when I try
to access it from somewhere else I get a corrupted string.
class someclass
const char* cat
somefunction{
const char* cat = const char* somestring;
}
when I write out cat I get the value I'm expecting which is in somestring.
When I try to access this value from somewhere else though someclass.cat I
get the first half part of the message as garbled "p 0 p 0 est of message"
when it shoudl say "rest of message". Any idea what the problem is?
- Dan
"Dan" wrote in message
news:47828be9$1@newsgroups.borland.com...
> I'm having a problem where I give a const char* a value and then when I
> try to access it from somewhere else I get a corrupted string.
>
> class someclass
> const char* cat
>
> somefunction{
> const char* cat = const char* somestring;
> }
> when I write out cat I get the value I'm expecting which is in somestring.
>
> When I try to access this value from somewhere else though someclass.cat I
> get the first half part of the message as garbled "p 0 p 0 est of message"
> when it shoudl say "rest of message". Any idea what the problem is?
>
> - Dan
Just making a final post to let everyone know that the problem has been
solved.
"Dan" writes:
> void NexusError(NxsString msg, /* the error message to be displayed */
> file_pos pos, /* the current file position */ long line, /* the
> current file line */ long col) /* the current column within the
> current file line */
> {
> ofstream TestError ("TestError.txt", ios::out);
It's a good idea to test that your stream properly opened before you
use it.
if (!TestError.good())
{
// handle error / unable to open file
}
> ErrorMsgC = (const char*)msg.c_str();
I'm not sure what an NxsString is, but since it's some sort of string
class, the problem is still clear. You're calling c_str() on it,
which returns a pointer to internal data of the string object. That's
fine, but only as long as that internal data is valid. It is *not* ok
to store that pointer in a member variable, however, because the
NxsString is passed by value to your function, meaning it will go out
of scope as soon as the function exits. Even if it's reference
counted (again I don't know what NxsString is)--which may extend the
lifetime of the data provided another string holds a reference--that
still doesn't solve the problem because 1) there might not be another
reference in the first place, or 2) even if there is another
reference, it may not exist by the time you later access the address
held in ErrorMsgC.
The only good option is to make a copy of the actual data you plan to
store, and not to merely remember the address of the data that is held
inside the string that was provided. The difference is that you will
own the memory in which the data is copied, and so can control how
long it lives, and can then guarantee it's handled correctly (provided
you handle it correctly.) That is, you won't be relying on external
behavior being what you hope it is.
The easiest way to do that is to not store a pointer, and change
ErrorMsgC to be a string object, perhaps even an NxsString. (Standard
C++ has its own string class, as does the VCL.) Anyway, the important
thing is that you allocate memory and copy the data into that memory,
which a string class does, and a raw pointer does not.
> return;
> }
BTW, a return statement at the end of a function is not necessary.
--
Chris (TeamB);
" Bruce Salzman" wrote in message
news:4782aa56$1@newsgroups.borland.com...
>
> "Dan" wrote in message
> news:478299be$1@newsgroups.borland.com...
>>
>> Here's the code below. When I check the file TestError.txt I see the
>> error messages have printed out as they should have. When I check
>> WriteErr.txt though, which was written by the WriteError() function the
>> error is written out as garbled as I described before. What am I doing
>> wrong? I figure it must be some simple thing in the way that C++ works,
>> (I'm used to Delphi).
>>
>
> You never actually call WriteError() in your example code.
>
> --
> Bruce
It is called from another part of the program which makes an instance of
this class, then does instance.WriteError(). Origionally I thought that the
"string" was getting corrupted between the caller and the instance, thus I
made WriteErr() or check if by writing it out in the same instance it would
work. It didnt'. It was just a diagnostic piece of code. But it doesn't
matter now as the problem is solved.
- Dan
"Dan" wrote in message
news:478299be$1@newsgroups.borland.com...
>
> Here's the code below. When I check the file TestError.txt I see the error
> messages have printed out as they should have. When I check WriteErr.txt
> though, which was written by the WriteError() function the error is written
> out as garbled as I described before. What am I doing wrong? I figure it
> must be some simple thing in the way that C++ works, (I'm used to Delphi).
>
You never actually call WriteError() in your example code.
--
Bruce
"Dan" wrote in message
news:47828be9$1@newsgroups.borland.com...
> I'm having a problem where I give a const char* a value and then when I
> try to access it from somewhere else I get a corrupted string.
>
> class someclass
> const char* cat
>
> somefunction{
> const char* cat = const char* somestring;
> }
> when I write out cat I get the value I'm expecting which is in somestring.
>
> When I try to access this value from somewhere else though someclass.cat I
> get the first half part of the message as garbled "p 0 p 0 est of message"
> when it shoudl say "rest of message". Any idea what the problem is?
>
> - Dan
Well I'm not really sure what the problem was but I just settled on passing
the whole error class as far as I could in the dll before turning it into a
const char* and sending it back to delphi, it worked, problem solved.
- Dan
"Chris Uzdavinis (TeamB)" wrote in message
news:861w8ts5e3.fsf@explicit.atdesk.com...
> "Dan" writes:
>
>> I'm having a problem where I give a const char* a value and then when
>> I try to access it from somewhere else I get a corrupted string.
>>
>> class someclass
>> const char* cat
>>
>> somefunction{
>> const char* cat = const char* somestring;
>> }
>> when I write out cat I get the value I'm expecting which is in
>> somestring.
>>
>> When I try to access this value from somewhere else though
>> someclass.cat I get the first half part of the message as garbled "p 0
>> p 0 est of message" when it shoudl say "rest of message". Any idea
>> what the problem is?
>
> Probably them memory in which you write the data is no longer valid.
>
> If you show actual program code that reliably causes the problem we
> can point you to a more specific problem. Look for returning pointers
> to local variables, or pointers into deleted objects. Also, look for
> modifications of string literals, which is undefined behavior.
>
> --
> Chris (TeamB);
Here's the code below. When I check the file TestError.txt I see the error
messages have printed out as they should have. When I check WriteErr.txt
though, which was written by the WriteError() function the error is written
out as garbled as I described before. What am I doing wrong? I figure it
must be some simple thing in the way that C++ works, (I'm used to Delphi).
- Dan
#include "ncl.h"
#include "String.h"
#include
//NxsTaxaBlock *taxa = NULL;
//NxsTreesBlock *trees = NULL;
class MyReader : public NxsReader
{
public:
ifstream inf;
ofstream outf;
const char* ErrorMsgC;
int ErrorLine;
MyReader() : NxsReader()
{
// inf and outf are opened by the code that uses this class
}
~MyReader()
{
inf.close();
outf.close();
}
void SkippingDisabledBlock(NxsString blockName) {}
void WriteError()
{
ofstream WriteErr ("WriteErr.txt", ios::out);
WriteErr << "start" << endl;
WriteErr << ErrorMsgC << endl;
WriteErr.close();
}
void OutputComment(const NxsString &msg)
{
outf << msg;
}
void NexusError(NxsString msg, /* the error message to be displayed */
file_pos pos, /* the current file position */ long line, /* the current file
line */ long col) /* the current column within the current file line */
{
ofstream TestError ("TestError.txt", ios::out);
TestError << "okaythen" << endl;
TestError << "There as an error at line " << line;
TestError << ", column " << col;
TestError << " (file position " << pos << "): " << endl;
TestError << msg << endl;
TestError.close();
outf << "error encountered!!!!!!!!!!!!!!!!" << endl;
ErrorMsgC = (const char*)msg.c_str();
ErrorLine = line;
outf << "here's the msg.c_str() let's see what we get: " << msg.c_str()
<< " , And now the ErrorMsg: " << ErrorMsgC << endl;
return;
}
};
"Dan" writes:
> I'm having a problem where I give a const char* a value and then when
> I try to access it from somewhere else I get a corrupted string.
>
> class someclass
> const char* cat
>
> somefunction{
> const char* cat = const char* somestring;
> }
> when I write out cat I get the value I'm expecting which is in somestring.
>
> When I try to access this value from somewhere else though
> someclass.cat I get the first half part of the message as garbled "p 0
> p 0 est of message" when it shoudl say "rest of message". Any idea
> what the problem is?
Probably them memory in which you write the data is no longer valid.
If you show actual program code that reliably causes the problem we
can point you to a more specific problem. Look for returning pointers
to local variables, or pointers into deleted objects. Also, look for
modifications of string literals, which is undefined behavior.
--
Chris (TeamB);