Click here to Skip to main content
12,079,140 members (46,640 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ VisualC++
Hi,

This is a simple set Function, I am trying to set a string from one class to another class using a simple function.

void setStringVariable(string stringtext)
{
   m_stringTest = stringtext;
}
When I call the function, I get ACCESS_VIOLATION ERROR.
I am confused, what is going wrong?

Any Help.
Posted 4-Dec-12 10:31am
Edited 4-Dec-12 10:33am
v2
Comments
André Kraak 4-Dec-12 15:36pm
   
How are you calling the setStringVariable function?
gssajith87 4-Dec-12 15:57pm
   
setStringVariable is in a class as public member, i am calling using the object of the class.
André Kraak 4-Dec-12 16:00pm
   
Which specific call off the function is causing the problem?
What is the parameter you are passing to the function at that moment?
   
Also, is it std::string, or something else?
Why having this function at all? Strings are assignable without the help of such function. :-)
--SA
JackDingler 4-Dec-12 16:23pm
   
Which line is it failing on?

Are you crossing DLL boundaries?
Saintd81 4-Dec-12 17:03pm
   
What is the protection level of m_stringTest?
gssajith87 4-Dec-12 17:06pm
   
Yes it is a std::string

No it is within a DLL,

extern c {

DECLDIR BOOL FUNC1( void *inputMgr, void* sdelegate)
{

Func2 *func = new Func2();
func->setStringVariable(stringvalue);
}
}

If i comment out this setStringVariable Function it is working. setStringVariable just assign the string to member variable of that class.
nv3 4-Dec-12 18:30pm
   
Please show the code of the Func2 class as well. I would guess that the problem is burried there.
Andrew Cherednik 4-Dec-12 20:28pm
   
what is stringvalue in your code?
gssajith87 4-Dec-12 21:42pm
   
class Func2
{
private:
string m_stringTest;
public:
Func2()
{
m_stringTest = "";
}
void setStringValue(string stringText)
{
m_stringTest = stringText;
}
}
Philippe Mori 4-Dec-12 23:11pm
   
Usually, you should be able to find the problem in seconds using a debugger...

You can try to replace m_stringTest = stringText; by either m_stringTest = "something"; or by std::string localCopy = stringText; and see if one of those also fails. If the first case, it would mean that the this pointer is corrupted while in the second case, it would mean that the original string is corrupted.
gssajith87 5-Dec-12 2:40am
   
Thanks All, i finally got it fixed, i changed setStringValue to take char * as argument.
char *buff;
void setStringValue(char *buffText)
{
buff = new char[strlen(buffText)+1];
memset(buff, 0, strlen(buffText) +1);
memcpy(buff, buffText, strlen(buffText));
}
Note to delete the buff in the destructor of the class.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Thanks All, i finally got it fixed, i changed setStringValue to take char * as argument.

char *buff;
 
void setStringValue(char *buffText)
{
  buff = new char[strlen(buffText)+1];
  memset(buff, 0, strlen(buffText) +1);
  memcpy(buff, buffText, strlen(buffText));
}

Note to delete the buff in the destructor of the class
  Permalink  
Comments
Stefan_Lang 5-Dec-12 4:31am
   
Have you considered the possibility that the cause of that access violation is passing a NULL pointer to setStringValue? Doing so will not break the code you posted here, but it will still cause unexpected behaviour!
gssajith87 5-Dec-12 9:42am
   
@Stefan_Lang, It was not the NULL pointer i was passing to, have verified the same.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Your original code should work unless the parameter you are passing to the setStringValue function is a private datamember of some other class.
  Permalink  
Comments
gssajith87 5-Dec-12 9:46am
   
Hi, the same call from another function works fine, when i call it from the second function, the ACCESS_VIOLATION comes in, in all the ways, both call is similar, but fails in the second. Not able to find the reason for crash, it was weird.
gssajith87 5-Dec-12 9:52am
   
And one more information, as u asked, the variable that i am calling is a local variable of the calling function
Aswin Waiba 6-Dec-12 1:04am
   
Try this

void setStringValue(string stringText)
{
if(stringText.length())
m_stringTest = stringText;
else
cout<<"error";
}

This should make it clear if you're passing a corrupted string.
gssajith87 7-Dec-12 12:35pm
   
Thanks Aswin, I tried the same , still had issues.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web03 | 2.8.160212.1 | Last Updated 5 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100