Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Moved] [Solved] Crash when taking the address of local variables
Qt 6.11 is out! See what's new in the release blog

[Moved] [Solved] Crash when taking the address of local variables

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 3.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jmper
    wrote on last edited by
    #1

    Edit: moved to the General forum -- peppe

    @// foo.h
    class foo {
    public:
    QDataStream m_io;

    foo();
    ~foo();

    bool init(const QString filename);
    void check();

    private:

    };

    // foo.cpp
    foo::foo()
    {

    }

    foo::~foo()
    {

    }

    bool foo::init(const QString filename)
    {
    QFile myfile(filename);
    if (!myfile.open(QIODevice::ReadWrite))
    return false;

    m_io.setDevice(&myfile);
    m_io.setByteOrder(QDataStream::BigEndian);
    
    return true;
    

    }

    void foo::check()
    {
    qint32 mhead;
    m_io >> mhead; // this line causes a segfault :(
    }

    // main.cpp
    int main()
    {
    foo my_foo;

    my_foo.init("c:\some\file.dat");
    my_foo.check(); // crash due to segfault

    return 0;
    }@

    It's seems to segfault whenever I try and access m_io in any other function than the one it was created in.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      Is init() returning true or false?

      (Also, please use more descriptive titles for your threads.)

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jmper
        wrote on last edited by
        #3

        Sorry I really didn't know what to title it :( my bad.

        init returns true, and then steps into the other function, but then segfaults at @ m_io >> mhead; // this line causes a segfault :(@

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #4

          You're creating the QFile on the stack in init(). It goes away after init() exits. The QDataStream is then pointing to an invalid QFile object internally, thus the crash.

          Move your QFile into your class definition as a member variable.

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jmper
            wrote on last edited by
            #5

            Oh man :s I didn't even think of that, thanks, works perfect.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #6

              No problem! Be sure and edit the title of the post to add [Solved] at the beginning. Thanks!

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved