Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. About preventing memory leaks

About preventing memory leaks

Scheduled Pinned Locked Moved C / C++ / MFC
helpcomtoolsperformancequestion
6 Posts 5 Posters 0 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.
  • M Offline
    M Offline
    Marco Bertschi
    wrote on last edited by
    #1

    I have a problem regarding file streams in Qt. Basically, I read a file using this code:

    int LoadSettings(){
    QFile* settingsFile = new QFile(storageFileLocation);

    if(!settingsFile->open(QIODevice::ReadOnly | QIODevice::Text)){
        return SettingsStorage::ErrorReadingFile;
    }
    
    QTextStream settingsFileStream(settingsFile);
    
    
    while(!settingsFileStream.atEnd()){
       QString fileLine;
       fileLine = settingsFileStream.readLine();
       QStringList splittedSettings = fileLine.split(SETTING\_NAME\_VALUE\_SEPARATOR);
       int addingStatus = AddSettingIfNotExist(splittedSettings\[SETTINGSARRAY\_SETTINGNAMEPOS\], splittedSettings\[SETTINGSARRAY\_SETTINGVALUEPOS\]);
    }
    
    delete(settingsFile);//Memory access violation
    
    return SettingsStorage::Ok;
    

    }

    AFAIK I have to delete dynamically allocated memory myself. I dynamically allocate the QFile* "settingsFile" and want to delete it as soon as I do not need it anymore. So why do I get a memory access violation message? I also know that Qt automatically disposes all allocated objects as soon as they derive from the QObject base class, but I want to write to the file later, therefore I have to clean up the allocated memory by myself. Any help is greatly appreciated.

    cheers Marco Bertschi


    Twitter | Articles


    You have absolutely no idea how glad I am that I have no idea at all. - OriginalGriff I'm at peace with the world and myself. - Me

    F N J 3 Replies Last reply
    0
    • M Marco Bertschi

      I have a problem regarding file streams in Qt. Basically, I read a file using this code:

      int LoadSettings(){
      QFile* settingsFile = new QFile(storageFileLocation);

      if(!settingsFile->open(QIODevice::ReadOnly | QIODevice::Text)){
          return SettingsStorage::ErrorReadingFile;
      }
      
      QTextStream settingsFileStream(settingsFile);
      
      
      while(!settingsFileStream.atEnd()){
         QString fileLine;
         fileLine = settingsFileStream.readLine();
         QStringList splittedSettings = fileLine.split(SETTING\_NAME\_VALUE\_SEPARATOR);
         int addingStatus = AddSettingIfNotExist(splittedSettings\[SETTINGSARRAY\_SETTINGNAMEPOS\], splittedSettings\[SETTINGSARRAY\_SETTINGVALUEPOS\]);
      }
      
      delete(settingsFile);//Memory access violation
      
      return SettingsStorage::Ok;
      

      }

      AFAIK I have to delete dynamically allocated memory myself. I dynamically allocate the QFile* "settingsFile" and want to delete it as soon as I do not need it anymore. So why do I get a memory access violation message? I also know that Qt automatically disposes all allocated objects as soon as they derive from the QObject base class, but I want to write to the file later, therefore I have to clean up the allocated memory by myself. Any help is greatly appreciated.

      cheers Marco Bertschi


      Twitter | Articles


      You have absolutely no idea how glad I am that I have no idea at all. - OriginalGriff I'm at peace with the world and myself. - Me

      F Offline
      F Offline
      Freak30
      wrote on last edited by
      #2

      Are you sure that the problem is settingsFile? If QObjects really dispose themselves, maybe they assume to be allocated on the heap and give a problem than being allocated on the stack. Have you tried commtenting parts of the code out, especially the loop and the QTextStream?

      M 1 Reply Last reply
      0
      • F Freak30

        Are you sure that the problem is settingsFile? If QObjects really dispose themselves, maybe they assume to be allocated on the heap and give a problem than being allocated on the stack. Have you tried commtenting parts of the code out, especially the loop and the QTextStream?

        M Offline
        M Offline
        Marco Bertschi
        wrote on last edited by
        #3

        Freak30 wrote:

        Are you sure that the problem is settingsFile?

        Yes. And every other dynamically allocated object. I can allocate memory for an object, but not delete it.

        Freak30 wrote:

        Have you tried commtenting parts of the code out, especially the loop and the QTextStream?

        Commented out the delete-part and everything was fine. Except that there was a memory leakage.

        cheers Marco Bertschi


        Twitter | Articles


        You have absolutely no idea how glad I am that I have no idea at all. - OriginalGriff I'm at peace with the world and myself. - Me

        1 Reply Last reply
        0
        • M Marco Bertschi

          I have a problem regarding file streams in Qt. Basically, I read a file using this code:

          int LoadSettings(){
          QFile* settingsFile = new QFile(storageFileLocation);

          if(!settingsFile->open(QIODevice::ReadOnly | QIODevice::Text)){
              return SettingsStorage::ErrorReadingFile;
          }
          
          QTextStream settingsFileStream(settingsFile);
          
          
          while(!settingsFileStream.atEnd()){
             QString fileLine;
             fileLine = settingsFileStream.readLine();
             QStringList splittedSettings = fileLine.split(SETTING\_NAME\_VALUE\_SEPARATOR);
             int addingStatus = AddSettingIfNotExist(splittedSettings\[SETTINGSARRAY\_SETTINGNAMEPOS\], splittedSettings\[SETTINGSARRAY\_SETTINGVALUEPOS\]);
          }
          
          delete(settingsFile);//Memory access violation
          
          return SettingsStorage::Ok;
          

          }

          AFAIK I have to delete dynamically allocated memory myself. I dynamically allocate the QFile* "settingsFile" and want to delete it as soon as I do not need it anymore. So why do I get a memory access violation message? I also know that Qt automatically disposes all allocated objects as soon as they derive from the QObject base class, but I want to write to the file later, therefore I have to clean up the allocated memory by myself. Any help is greatly appreciated.

          cheers Marco Bertschi


          Twitter | Articles


          You have absolutely no idea how glad I am that I have no idea at all. - OriginalGriff I'm at peace with the world and myself. - Me

          N Offline
          N Offline
          Newbie00
          wrote on last edited by
          #4

          Why you are using "delete(settingsFile);" instead of "delete settingsFile;". What does it mean? I think you should also close settings file before deleting it. And if the settingsfile->open() fails you will get another memmory leaks, because you are leaving function without deleting settingsfile.

          D 1 Reply Last reply
          0
          • N Newbie00

            Why you are using "delete(settingsFile);" instead of "delete settingsFile;". What does it mean? I think you should also close settings file before deleting it. And if the settingsfile->open() fails you will get another memmory leaks, because you are leaving function without deleting settingsfile.

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Newbie00 wrote:

            What does it mean?

            The same this as sizeof(var) and return(result). Parenthesis are optional.

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

            1 Reply Last reply
            0
            • M Marco Bertschi

              I have a problem regarding file streams in Qt. Basically, I read a file using this code:

              int LoadSettings(){
              QFile* settingsFile = new QFile(storageFileLocation);

              if(!settingsFile->open(QIODevice::ReadOnly | QIODevice::Text)){
                  return SettingsStorage::ErrorReadingFile;
              }
              
              QTextStream settingsFileStream(settingsFile);
              
              
              while(!settingsFileStream.atEnd()){
                 QString fileLine;
                 fileLine = settingsFileStream.readLine();
                 QStringList splittedSettings = fileLine.split(SETTING\_NAME\_VALUE\_SEPARATOR);
                 int addingStatus = AddSettingIfNotExist(splittedSettings\[SETTINGSARRAY\_SETTINGNAMEPOS\], splittedSettings\[SETTINGSARRAY\_SETTINGVALUEPOS\]);
              }
              
              delete(settingsFile);//Memory access violation
              
              return SettingsStorage::Ok;
              

              }

              AFAIK I have to delete dynamically allocated memory myself. I dynamically allocate the QFile* "settingsFile" and want to delete it as soon as I do not need it anymore. So why do I get a memory access violation message? I also know that Qt automatically disposes all allocated objects as soon as they derive from the QObject base class, but I want to write to the file later, therefore I have to clean up the allocated memory by myself. Any help is greatly appreciated.

              cheers Marco Bertschi


              Twitter | Articles


              You have absolutely no idea how glad I am that I have no idea at all. - OriginalGriff I'm at peace with the world and myself. - Me

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              Certainly looks like the entire code block is suspect to me. You have something that looks like a file, which is scoped by new/delete. Then that gets used by something else, that looks like a file reader, and the scope of that is the method. Thus it continues to have a reference to the 'file' until the method exits even though you have already deleted it. Either both should be on the heap or neither should be. And the first has a close method. Which, hopefully the dtor takes care of by certainly I never assume that. You make a memory allocation and then do an error check and exit - without cleaning up.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups