CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    CString causes crash???

    My program crashes with an error like access violation when the following statement is executed,
    Code:
    str_test = "Test CString";
    str_test is defined as CString. I have no clue why such a simple statement can cause a crash. Any guru here can tell me how to find out why? Thanks.

  2. #2
    Join Date
    Sep 2011
    Posts
    6

    Re: CString causes crash???

    Quote Originally Posted by LarryChen View Post
    My program crashes with an error like access violation when the following statement is executed,
    Code:
    str_test = "Test CString";
    str_test is defined as CString. I have no clue why such a simple statement can cause a crash. Any guru here can tell me how to find out why? Thanks.
    try out
    [code=cpp]
    CString * str_test="Test CString";
    [/code]

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: CString causes crash???

    Quote Originally Posted by Grasshopper.Network View Post
    try out
    [code=cpp]
    CString * str_test="Test CString";
    [/code]
    Why? I'm sure you mean well, but that makes no sense.

    Larry, no a simple assignment isn't going to crash a CString. With 625 posts and six years on the forum, you should be familiar with basic debugging techniques by now.

  4. #4
    Join Date
    Jul 2005
    Posts
    1,030

    Re: CString causes crash???

    Quote Originally Posted by GCDEF View Post
    Why? I'm sure you mean well, but that makes no sense.

    Larry, no a simple assignment isn't going to crash a CString. With 625 posts and six years on the forum, you should be familiar with basic debugging techniques by now.
    Debugging doesn't help much. It only points out a pointer contained by CString is invalid, so when this invalid pointer points to a literal string, it crashes. I just don't understand why the pointer contaied by the CString is invalid.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: CString causes crash???

    Quote Originally Posted by LarryChen View Post
    Debugging doesn't help much. It only points out a pointer contained by CString is invalid, so when this invalid pointer points to a literal string, it crashes. I just don't understand why the pointer contaied by the CString is invalid.
    I'm pretty sure if there was a bug like that MS would have found it and others would have reported. It's safe to say the problem is probably somewhere in your code where you've stepped off an array or something and corrupted it.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: CString causes crash???

    Quote Originally Posted by LarryChen View Post
    Debugging doesn't help much. It only points out a pointer contained by CString is invalid, so when this invalid pointer points to a literal string, it crashes. I just don't understand why the pointer contaied by the CString is invalid.
    1. What "the pointer contaied by the CString...do you mean?
    2. Do you somewhere (before the code you were showing is executed) call something like
      Code:
      str_test.GetBuffer(...);
      without the following ReleaseBuffer?
    Victor Nijegorodov

  7. #7
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: CString causes crash???

    Quote Originally Posted by LarryChen View Post
    Debugging doesn't help much. It only points out a pointer contained by CString is invalid, so when this invalid pointer points to a literal string, it crashes. I just don't understand why the pointer contaied by the CString is invalid.
    In addition to being able to debug, I would expect you also understand that it is nearly impossible to offer any help if you don't provide any context. How is str_test defined? Is it a local variable? A class member? In what context is the code you posted called?

    From your description, I would expect that it is a class member and you are calling this code on an invalid instance of the class. In that case, the CString doesn't exist, which explains the invalid internals of the CString reported in the debugger.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

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