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 8835879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:21:46+00:00 2026-06-14T09:21:46+00:00

I have a class ClientWindow. I have created several instances of it and appended

  • 0

I have a class ClientWindow. I have created several instances of it and appended the their pointers to a a list. If i try to show any of the windows however, I get “Segmentation fault (core dumped)” I keep the list of windows in a class called controller.

Here is my controller header file:

#ifndef CONTROLLER_H
#define CONTROLLER_H

#include "clientwindow.h"

class Controller
{
public:
    Controller();
    void createClients(int num);
    void showWindows();

private:
    QList<ClientWindow*> clWList;
    int size;
};

#endif // CONTROLLER_H

this is the cpp file:

#include "controller.h"

Controller::Controller()
{
}

void Controller::createClients(int num)
{
    size = num;
    for(int i = 0; i < size; i++)
    {
        ClientWindow cw;
        clWList.append(&cw);
    }
}

void Controller::showWindows()
{
    for(int i = 0; i < size; i++)
    {
        ClientWindow* cw = clWList.at(0);
        cw->show();
    }
}

this is my main:

#include <QtGui/QApplication>
#include "clientwindow.h"
#include "controller.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
   // ClientWindow w;
  // w.show();

    QString temp = argv[1];
    bool ok;
    int tempI = temp.toInt(&ok, 10);
    Controller c;
    c.createClients(tempI);
    c.showWindows();



    return a.exec();
}
  • 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-06-14T09:21:47+00:00Added an answer on June 14, 2026 at 9:21 am

    This is where it goes wrong:

    for(int i = 0; i < size; i++)
    {
        ClientWindow cw;
        clWList.append(&cw);
    }
    

    A local variable cw is created on the stack in each iteration. It is deallocated at the end of each iteration. Meaning the data is gone. So you end up storing pointers pointing to junk.

    Calling a member function of a junk typically results in crash. 🙂 Do this instead:

    for(int i = 0; i < size; i++)
    {
        ClientWindow * cw = new ClientWindow();
        clWList.append(cw);
    }
    

    You’ll have to go through the list and delete the objects after you are done with them.

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

Sidebar

Related Questions

I have class MasterData whose instances contain some general information about all possible gadgets.
I have :- Class Abc extends Activity(){ private ActivityManager am; private List<RunningAppProcessInfo> rList; @Override
I have class List < ActiveRecord::Base has_many :list_items class ListItem < ActiveRecord::Base belongs_to :list
I have class which has Map, but when I try to presist this entity,
I have class method that returns a list of employees that I can iterate
I have: class Car {..} class Other{ List<T> GetAll(){..} } I want to do:
I have class A derived from a Windows.Form class, using the VS2008 designer. When
I have class called GroupSelect and made a collection List(Of GroupSelect)() . Now I
I have class called GroupSelect and made a collection List(Of GroupSelect)() . Now I
I have Class and Student objects. Both have collection of another as property. Which

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.