using std::string in VC++.Net causes exception.

D

doubts

Hi all,
I am trying to convert my bulk of code from VC++ 6.0 to VC++.Net.
when using std::string type variable, the application causes exception
at one instance and does not cause an exception at other.

i have two functions in the same .cpp file

//exception occurs at this function
void call(const char* var2 )
{
std::string var1;

( var2 == NULL ) ? var1= "itsme": var1= var2 ; //exception occurs at
this line "var1 = "itstime"
.......
.....
}

//exception does not occur at this function
void call1(const char* var2 )
{
std::string var1;

( var2 == NULL ) ? var1= "itsme": var1= var2 ;
.......
.....
}

the exception is CRT exception. There is some problem regarding
allocation which i am not able to figure out.This error happens at
several other places when i am trying to assign a value to string eg.
(string temp = " hi bye" ).
please help me reagarding this..

regards,
doubts.
 
T

Tom Widmer [VC++ MVP]

doubts said:
Hi all,
I am trying to convert my bulk of code from VC++ 6.0 to VC++.Net.

Is that 2002 or 2003? Why not go the whole hog and use VC++8, e.g.
Visual Studio 2005 (no .NET in the name this time!).
when using std::string type variable, the application causes exception
at one instance and does not cause an exception at other.

i have two functions in the same .cpp file

//exception occurs at this function
void call(const char* var2 )
{
std::string var1;

( var2 == NULL ) ? var1= "itsme": var1= var2 ; //exception occurs at
this line "var1 = "itstime"

That would normally be written:

var1 = ( var2 == NULL ) ? "itsme": var2 ;

but the semantics are the same either way.
......
....
}

//exception does not occur at this function
void call1(const char* var2 )
{
std::string var1;

( var2 == NULL ) ? var1= "itsme": var1= var2 ;
......
....
}

the exception is CRT exception. There is some problem regarding
allocation which i am not able to figure out.This error happens at
several other places when i am trying to assign a value to string eg.
(string temp = " hi bye" ).
please help me reagarding this..

The code you've posted is fine, if unorthodox. That means that the
problem is likely to be heap corruption (when you allocate or deallocate
memory from the heap, it sometimes manages to detect corruption that has
occured at some previous time). Possible causes of heap corruption are
bugs in your code (such as double deletes, uses of uninitialized or
deleted pointers, array bounds overwrites) or in your configuration
(mixing modules (exes and dlls) using different DLL CRTs, or using
static CRTs).

Tom
 
R

Ray

doubts said:
Hi all,
I am trying to convert my bulk of code from VC++ 6.0 to VC++.Net.

Define conversion? Your bulk of code is still native C++ to native C++,
yes? (That is no managed C++/CLI stuff?)
when using std::string type variable, the application causes exception
at one instance and does not cause an exception at other.

Try stepping into the function that causes the exception, and examine
the values of the variables involved just before you execute the line
that triggers the exception--I wonder which exactly CRT exception you
are referring to?
i have two functions in the same .cpp file

//exception occurs at this function
void call(const char* var2 )
{
std::string var1;

( var2 == NULL ) ? var1= "itsme": var1= var2 ; //exception occurs at
this line "var1 = "itstime"
......
....
}

//exception does not occur at this function
void call1(const char* var2 )
{
std::string var1;

( var2 == NULL ) ? var1= "itsme": var1= var2 ;
......
....
}

the exception is CRT exception. There is some problem regarding
allocation which i am not able to figure out.This error happens at
several other places when i am trying to assign a value to string eg.
(string temp = " hi bye" ).

I agree with Tom, your code looks OK, although personally I wouldn't
code it that way. Assigning a value to a string shouldn't raise a CRT
exception under normal situations, so are you sure that this is
isolated? I think it's likely that it's not this assignment per se that
causes the exception, rather it's something else.

Cheers
Ray
 
A

Andy

I noticed that when I assign string literals to string variables in
VC++.NET, I have to convert them into objects first...

ie:

( var2 == NULL ) ? var1= new System::String("itsme"): var1= var2 ;

I used System::String in your example, but you may need to use
std::string(). System::String is based on the standard template
library and should be compatible with your code.

Andy

B.T.W.
I'm having trouble accessing an unmanaged long from a managed class in
VC++.NET

When I do, the contents of the variable seem to be mangled. If I
access the same variable byte-by-byte, I get the correct value.
Regardless what I set the variable to, the value that is returned for a
long is always the same value. What's going on...can anyone help me?

A short version of the code follows:



//HEADER
namespace MyProgram
{

#pragma unmanaged
__nogc class unmanagedClass
{
public:unmanagedClass(); //CONSTRUCTOR

public: union{
struct {
long unmanagedLong; //UNMANAGED VARIABLE
} myData;
struct {
char bytes[4];
} b_myData;
} myUnion;
};


#pragma managed
public __gc class managedClass
{
public:managedClass(); //CONSTRUCTOR
private: unmanagedClass __nogc *ptrUnmanagedClass; //PTR TO UNMANAGED
CLASS
public:System::String* Get_unmanagedLong(); //METHOD TO GET
UNMANAGED VARIAHBLE
};
}



//CPP LISTING

//CONSTRUCTORS
MyProgram::unmanagedClass::unmanagedClass(){
unmanagedLong=1536; //HEX #0600
}
MyProgram::managedClass::managedClass(){
ptrUnmanagedClass=new unmanagedClass();
}


System::String* MyProgram::managedClass::Get_unmanagedLong(){
System::String *result;

//NEXT RETURNS CORRECT VALUE OF #0600
result=System::String::Concat(

System::String::Format("{0:x}",__box(ptrUnmanagedClass->myUnion.b_myData.bytes[0])),

System::String::Format("{0:x}",__box(ptrUnmanagedClass->myUnion.b_myData.bytes[1])),

System::String::Format("{0:x}",__box(ptrUnmanagedClass->myUnion.b_myData.bytes[2])),

System::String::Format("{0:x}",__box(ptrUnmanagedClass->myUnion.b_myData.bytes[3])));

//NEXT RETURNS INCORRECT VALUE OF #73CB6A62
result=System::String::Format("{0:x}",__box(ptrUnmanagedClass->myUnion.myData.unmanagedLong));

return(result);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top