CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Hybrid View

  1. #1
    Join Date
    Jun 2009
    Posts
    118

    Question string assignment crash

    Hi All,

    I have the following class declaration from which other classes are derived:

    Code:
    	class A
    	{
    	public:
    		A();
    		virtual ~A(){}
                    void function1(const char* fileName1,
    						       const char* fileName2,
    						       string _folderPath1,
    						       string _folderPath2 = "");
    	protected:
    		string s1;
    		string s2;
    	};
    where
    Code:
    string _folderPath2
    argument in member function1() has a default value.

    In the body of A::function1(const char* fileName1,
    const char* fileName2,
    string _folderPath1,
    string _folderPath2),
    Code:
      s1 = _folderPath1; //this line does not crash
      if(_folderPath2 != "")
      {
         s2 = _folderPath2; //this line crashes
      }
    In the code fragment above, as you can see, both s1 and s2 are protected members whose values are used by the derived classes. However, the line which assigns _folderPath1 to s1 does not crash whereas the one which assigns _folderPath2 to s2 crashes.

    To me, this seems to be a problem concerning s2 itself. But, I don 't know why.

    If you think that further information is needed to isolate the problem, please let me know (I can not list all my source code here since it is long)

    Any help will be appreciated.

    Thanks.
    Last edited by aryan1; December 15th, 2009 at 10:20 AM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: string assignment crash

    (I can not list all my source code here since it is long)
    Which is probably why your program crashes. It is the code you're not showing us that is causing the problem.

    The code you're showing has no problems with it. Crashes are runtime issues, where the program is in a certain state due to bad data the program is using plus bad programming. This means that we would need to see the program and what data you're using when running the program.

    You have probably corrupted memory in some way, and without all of the code, we can't tell you where.

    Regards,

    Paul McKenzie

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: string assignment crash

    If you have access to it, run your program through valgrind. That should help you identify where you're trashing that string.

  4. #4
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: string assignment crash

    There are so many problems that could cause the program to crash at a seemingly benign line of source code that they are too many too list. Perhaps the object in question was deleted and you are invoking a function with a dangling pointer to a deleted object? That would be one of many problems that might result in the software crashing at a line of code that seems reasonable. As Paul has pointed out, we need to see a program that demonstrates the problem. The class code itself doesn't appear to be the problem. It must be something with the code that manages those objects. I don't really see how you could mess up a string such that an assignment would fail since those string objects manage themselves and can only be updated through their interface. however the strings use heap memory so if it is a dangling pointer problem with the object instance it would make perfect sense for the code to crash during the string assignment. But again, that is just one of many other run-time problems that could exist.

    The process of creating a small program that uses the class in question might help you solve the problem before posting. First it would prove to you that there is nothing wrong with the class itself or you might find a problem with the class itself. If the former is true, then you would start looking at the code that uses the class so that you could try and update your example to simulate what the real application is doing with the object instances. Just make a test project and build that class along with some simple main function that builds some objects and calls functions on them and see what happens.
    Last edited by kempofighter; December 15th, 2009 at 02:00 PM. Reason: added 2nd paragraph

  5. #5
    Join Date
    Jun 2009
    Posts
    118

    Re: string assignment crash

    I totally understand that you can not tell anything specific without seeing the source code itself.

    However, I will follow the guidelines you all provided to isolate and resolve the problem.

    By the way, I got the exactly same code to run on two other machines, but not the one where the problem occurs.

    Might the problem be stemming from the OS installation ? Is it possible ? Or May "undefined behaviour" or some kind of memory leak still cause the problem ?

    The machine where the problem occurs has a much better system configuration (memory, CPU, etc) than the others where I got the same code to run successfully ...

    Quote Originally Posted by kempofighter View Post
    There are so many problems that could cause the program to crash at a seemingly benign line of source code that they are too many too list. Perhaps the object in question was deleted and you are invoking a function with a dangling pointer to a deleted object? That would be one of many problems that might result in the software crashing at a line of code that seems reasonable. As Paul has pointed out, we need to see a program that demonstrates the problem. The class code itself doesn't appear to be the problem. It must be something with the code that manages those objects. I don't really see how you could mess up a string such that an assignment would fail since those string objects manage themselves and can only be updated through their interface. however the strings use heap memory so if it is a dangling pointer problem with the object instance it would make perfect sense for the code to crash during the string assignment. But again, that is just one of many other run-time problems that could exist.

    The process of creating a small program that uses the class in question might help you solve the problem before posting. First it would prove to you that there is nothing wrong with the class itself or you might find a problem with the class itself. If the former is true, then you would start looking at the code that uses the class so that you could try and update your example to simulate what the real application is doing with the object instances. Just make a test project and build that class along with some simple main function that builds some objects and calls functions on them and see what happens.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: string assignment crash

    Quote Originally Posted by aryan1 View Post
    By the way, I got the exactly same code to run on two other machines, but not the one where the problem occurs.
    Doesn't really mean anything. A program that has bugs can seem to "work" on one system, and crash on another.
    Might the problem be stemming from the OS installation ? Is it possible ? Or May "undefined behaviour" or some kind of memory leak still cause the problem ?
    The issue is with your code -- never blame the OS or some other "outside force", unless you have documented proof that it is something with the compiler.

    Since you don't show any code, no one can help you any further except to tell you to "debug more thoroughly".
    The process of creating a small program that uses the class in question might help you solve the problem before posting.
    Did you do this? I can easily make your class do all sorts of wrong things, For example, what exactly do you do with those "filename" pointers? What if NULL is passed to your constructor for these pointers? At the very least, show us something that uses the class.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; December 16th, 2009 at 09:08 AM.

  7. #7
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: string assignment crash

    Quote Originally Posted by aryan1 View Post
    I totally understand that you can not tell anything specific without seeing the source code itself.

    However, I will follow the guidelines you all provided to isolate and resolve the problem.

    By the way, I got the exactly same code to run on two other machines, but not the one where the problem occurs.

    Might the problem be stemming from the OS installation ? Is it possible ? Or May "undefined behaviour" or some kind of memory leak still cause the problem ?

    The machine where the problem occurs has a much better system configuration (memory, CPU, etc) than the others where I got the same code to run successfully ...
    I'm betting on undefined behavior. I've seen some strange things in my life where a program with serious defects "appeared" to be working for many months and then one day for no evident reason it began to crash even on the same system that it had always executed on. In one instance, there was a spot where an uninitialized pointer was used to call functions and how it worked for months is beyond me. I was baffled by that one. In other instances I have seen where a pointer to a deleted object can be used to continue making function calls for some short time after the object is deleted. these kinds of problems are very difficult to find. Sometimes you have a race condition where it appears to work with one setup but not on another or it works for a while until something that seems unrelated is changed in the program and then you wonder why something else starts crashing. Welcome to C++ programming. Nothing surprises me any longer.

  8. #8
    Join Date
    Aug 2008
    Posts
    112

    Re: string assignment crash

    Quote Originally Posted by aryan1 View Post
    Hi All,

    I have the following class declaration from which other classes are derived:

    Code:
    	class A
    	{
    	public:
    		A();
    		virtual ~A(){}
                    void function1(const char* fileName1,
    						       const char* fileName2,
    						       string _folderPath1,
    						       string _folderPath2 = "");
    	protected:
    		string s1;
    		string s2;
    	};
    where
    Code:
    string _folderPath2
    argument in member function1() has a default value.

    In the body of A::function1(const char* fileName1,
    const char* fileName2,
    string _folderPath1,
    string _folderPath2),
    Code:
      s1 = _folderPath1; //this line does not crash
      if(_folderPath2 != "")
      {
         s2 = _folderPath2; //this line crashes
      }
    I find using (const) string too in place of const chars would make your coded functions healthy
    hi,,,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured