Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7676083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:07:02+00:00 2026-05-31T17:07:02+00:00

I’ve started learning qt recently and was playing with it, also remembering a bit

  • 0

I’ve started learning qt recently and was playing with it, also remembering a bit of c++ since I haven’t used it in a while…

Anyway, I made this simple test class to see what would happen when you have a dangling pointer:

#include <QtCore/QCoreApplication>

#include <iostream>

class Test{
public:
    QString str;
    int i;
public:
    Test(){
        i=1337;
        str="str";
        std::cout<<"initialized\n";
    }

    ~Test(){
        std::cout<<"destructor called\n";
        delete &str;
        delete &i;
        /*the program MAY run if the deletes aren't called;*/
    }
};

Test* p;

void test_function(){
    Test a;
    p=&a; //this will give a segfault, since the destructor of a is called at the end of this function's scope, but p still points to a
    //p=new Test(a); //this will call the copy constructor of p, copying the values from a to p
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    test_function();

    std::cout<<p->str.toStdString() << p->i<<std::endl;

    return a.exec();
}

which, sure enough, crashes (but it doesn’t say why – probably segfault). However, if I comment the line where the dangling pointer is called (the cout in main()), the program now crashes giving me a “std::bad_alloc” exception. The following list of things tested independently causes the exception to not be thrown anymore:

  • commenting the initialization of str;
  • commenting the destructor of str;
  • replacing QString with std::string;

What I tried and didn’t have any effect was initializing str with QString::fromStdString(“str”) (instead of just “str”).

I’ve also tried surrounding the initialization and destruction of str (while a QString) with a try/catch block, but to no avail. Also, try/catch for the test_function() – no result. I did however succeeded in catching the exception by surrounding the a.exec() in a try/catch block.

Why does it throw an exception when the cout is commented, but only crashes otherwise?
Why does having both initializer and destructor for str throw the exception, but commenting one or both only results in a crash? (now that I’m writing this, I realise this may be the sole problem; however, why does it crash for QString but not for std::string?)
Why does the a.exec() throw the exception?

Can someone who knows qt (or even c++) – better than I do – explain what’s going on?
Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:07:03+00:00Added an answer on May 31, 2026 at 5:07 pm

    This is one mistake:

    delete &str;
    delete &i;
    

    trying to delete variables that were not dynamically allocated, i.e. using new. Remove those two lines. i and str will be destroyed automatically when the Test instance is destructed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.