Results 1 to 14 of 14

Thread: freeing my memory (life should be lived forwards)

  1. #1
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default freeing my memory (life should be lived forwards)

    I allocated a buffer in my app like below:
    Qt Code:
    1. char* ptr = new char[variable]; // its has a variable size but this is known before calling this line
    To copy to clipboard, switch view to plain text mode 

    then i pass that pointer to my class

    Qt Code:
    1. myClass* c = new myClass(ptr);
    To copy to clipboard, switch view to plain text mode 

    in my myClass constructor and destructor
    Qt Code:
    1. myClass::myClass(char* ptr)
    2. {
    3. m_ptr = ptr; //m_ptr is a private char* pointer
    4. }
    5. myClass::~myClass
    6. {
    7. delete [] m_ptr;
    8. }
    To copy to clipboard, switch view to plain text mode 

    I expected this to free up the buffer everytime i deleted the object myClass but i was wrong. I am monitoring my app in task Manager and it is still using a lot of memory even after deleting myClass. please tell me where did i go wrong.

    baray98

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: freeing my memory (life should be lived forwards)

    Not here. The above code is fine.

  3. #3
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: freeing my memory (life should be lived forwards)

    one more thing I passed that same pointer to the inherited class will it matter (should i delete it there too?)

    how does a delete work ?

    if i have a
    Qt Code:
    1. char* ptr = new char[4];
    2. char* ptr1 = ptr;
    3.  
    4. //now if I delete ptr
    5. delete[] ptr;
    6.  
    7. //is ptr 1 still available or is the buffer allocated by new char[4] gone?
    8. //will this produce an exeption
    9. char c = *ptr1
    To copy to clipboard, switch view to plain text mode 

    baray98

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: freeing my memory (life should be lived forwards)

    ptr1 will point now to a freed memory location.
    So you shouldn't delete here too. Once is enough.

  5. #5
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: freeing my memory (life should be lived forwards)

    another puzzle but i think its related:

    when i allocated the buffer i used
    Qt Code:
    1. char* ptr = new char [1000];
    To copy to clipboard, switch view to plain text mode 

    then,

    i pass it to my class like this
    Qt Code:
    1. myClass* c = new myClass(ptr);
    To copy to clipboard, switch view to plain text mode 

    in the destructor of my myClass I did this
    Qt Code:
    1. myClass::~myClass()
    2. {
    3. int x = sizeof(m_ptr); //remember m_ptr was declared as char*
    4. delete[] m_ptr;
    5. }
    To copy to clipboard, switch view to plain text mode 

    int xis equal to four (4) bytes when i stopped (passed) there.

    hmmmm....

    baray98

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: freeing my memory (life should be lived forwards)

    sizeof() is a macro which is evaluated at compile time and not during execution. It will always be 4 regardles of what m_ptr holds. In the final binary there is a "4" constant instead of the sizeof call.

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: freeing my memory (life should be lived forwards)

    that's correct. the size of a pointer is 32 bits - it holds a memory address.
    To find the length of a string use strlen.

  8. #8
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: freeing my memory (life should be lived forwards)

    here is a snippet of my making the buffer (maybe you guys can figure something out)
    Qt Code:
    1. //reads frame from valid file
    2. SEECFrame::t_FrameWrapper* firstFrameWrapper = (SEECFrame::t_FrameWrapper*)
    3. new uint8[sizeof(SEECFrame::t_FrameWrapper)];
    4. int frameSize = 0;
    5. file.seek(0);
    6.  
    7. int filePos = 0;
    8. uint8* m_rawData = NULL;
    9. while (!file.atEnd())
    10. {
    11. //dig frames
    12. filePos = file.pos();
    13. stream.readRawData((char*)firstFrameWrapper, sizeof(SEECFrame::t_FrameWrapper));
    14. frameSize = firstFrameWrapper->lengthL +
    15. (firstFrameWrapper->lengthM << 8) +
    16. (firstFrameWrapper->lengthH<<16) ;
    17. file.seek(filePos);
    18.  
    19. m_rawData = new uint8 [(int)frameSize]; // where i made the buffer
    20. stream.readRawData((char*)m_rawData, (int)frameSize);
    21. MyFrame* frame = new MyFrame(m_rawData,this); //passed it to the class
    22. if (!frame->isValid())
    23. {
    24. err(tr("Frame No %1 in File : %2 is invalid \n Reason: %3")
    25. .arg(m_VectorFrames.count()+1)
    26. .arg(file.fileName()).arg(frame->errorString()));
    27. delete frame;
    28. delete m_rawData;
    29. break;
    30. }
    31. m_VectorFrames.append(frame);
    32.  
    33.  
    34. if (progress.wasCanceled())
    35. {
    36. closeFile();
    37. break;
    38. }
    39.  
    40. qApp->processEvents();
    41. }
    42. //cleanup firstFrameWrapper
    43. delete[] firstFrameWrapper;
    To copy to clipboard, switch view to plain text mode 

    myFrame Class constructor and dtor look like these

    Qt Code:
    1. myFrame::myFrame( const uint8* rawFrame, QObject * parent )
    2. :QObject(parent),m_Validity(false)
    3. {
    4. //ctor
    5. m_rawFrame = rawFrame;
    6. frameWrapper = (t_FrameWrapper*) m_rawFrame;
    7. m_errorString = QString::null;
    8. //determine which type of records
    9. initRecordType();
    10.  
    11. processFrame();
    12. }
    13.  
    14. myFrame::~myFrame()
    15. {
    16. //dtor
    17. if (m_rawFrame != NULL)
    18. {
    19. int x = sizeof(m_rawFrame);
    20. delete[] m_rawFrame;
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    this is in may app where i tried to clean up
    Qt Code:
    1. void UnpackTest::closeFile(void)
    2. {
    3. myFrame* frame = NULL;
    4. while (m_VectorFrames.count())
    5. {
    6. frame = m_VectorFrames.takeLast();
    7. delete frame;
    8. frame = NULL;
    9. }
    10. m_VectorFrames.clear();
    11.  
    12. gbFrameInfo->clear();
    13. gbSensorInfo->clear();
    14.  
    15. setWindowTitle("Unpacker");
    16. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: freeing my memory (life should be lived forwards)

    memory dump before the delete

    0xc1caa78: 4d 41 0d 1c 00 b7 01 08|02 3a 00 0d 8e 40 00 0c MA...·...:..Ž@..
    0xc1caa88: 03 5b 03 5e 03 9a 03 7d|03 47 03 46 03 4f 04 33 .[.^.š.}.G.F.O.3
    0xc1caa98: fd 2a 00 00 02 e7 e9 ed|00 16 01 00 44 41 e7 8c ý*...çéÃ*....DAçŒ
    0xc1caaa8: 3f 04 11 11 01 99 99 00|10 11 01 11 11 99 99 11 ?....™™......™™.
    0xc1caab8: 01 00 32 22 21 99 99 32|23 22 11 19 91 20 01 80 ..2"!™™2#"..‘ .€
    0xc1caac8: 53 35 31 55 31 33 33 17|33 55 33 35 53 35 11 55 S51U133.3U35S5.U
    0xc1caad8: 15 55 11 35 13 11 55 79|13 55 11 31 53 19 33 33 .U.5..Uy.U.1S.33
    0xc1caae8: 2f 0d f2 20 00 f2 f0 f0|00 df f0 00 f0 ff 20 0f

    after the delete execution

    0xc1caa78: 48 e6 1c 0c 90 01 f5 03|ee fe ee fe ee fe ee fe Hæ...õ.îþîþîþîþ
    0xc1caa88: ee fe ee fe ee fe ee fe|ee fe ee fe ee fe ee fe îþîþîþîþîþîþà ®Ã¾Ã®Ã¾
    0xc1caa98: ee fe ee fe ee fe ee fe|ee fe ee fe ee fe ee fe îþîþîþîþîþîþà ®Ã¾Ã®Ã¾
    0xc1caaa8: ee fe ee fe ee fe ee fe|ee fe ee fe ee fe ee fe îþîþîþîþîþîþà ®Ã¾Ã®Ã¾
    0xc1caab8: ee fe ee fe ee fe ee fe|ee fe ee fe ee fe ee fe îþîþîþîþîþîþà ®Ã¾Ã®Ã¾
    0xc1caac8: ee fe ee fe ee fe ee fe|ee fe ee fe ee fe ee fe îþîþîþîþîþîþà ®Ã¾Ã®Ã¾
    0xc1caad8: ee fe ee fe ee fe ee fe|ee fe ee fe ee fe ee fe îþîþîþîþîþîþà ®Ã¾Ã®Ã¾
    0xc1caae8: ee fe ee fe ee fe ee fe|ee fe ee fe

    does this means windows are just marking these memory instead of freeing them?

    baray98

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: freeing my memory (life should be lived forwards)

    Quote Originally Posted by baray98 View Post
    does this means windows are just marking these memory instead of freeing them?
    No, windows marks the freed memory, to help you detect accesses to it.

  11. #11
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: freeing my memory (life should be lived forwards)

    When do you delete the elements in m_vectorFrame?

  12. #12
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: freeing my memory (life should be lived forwards)

    marcel,

    in my closeFile function (this is actually a slot connected to signal closeFile (QAction) triggered()

    Qt Code:
    1. void UnpackTest::closeFile(void)
    2. {
    3. SEECFrame* frame = NULL;
    4. while (m_VectorFrames.count())
    5. {
    6. frame = m_VectorFrames.takeLast();
    7. delete frame;
    8. frame = NULL;
    9. }
    10. m_VectorFrames.clear();
    11.  
    12. gbFrameInfo->clear();
    13. gbSensorInfo->clear();
    14.  
    15. setWindowTitle("Unpacker");
    16. }
    To copy to clipboard, switch view to plain text mode 

    Jacek,

    No, windows marks the freed memory, to help you detect accesses to it.
    so its freed but why does my task manager is still showing that my app is eating a lot? please explain.....

    baray98

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: freeing my memory (life should be lived forwards)

    It's probably eating a lot elsewhere...

  14. #14
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: freeing my memory (life should be lived forwards)

    it seems that it has something to do with my class inherits QObject. I took them all out. and it seems to free up the memory?

    but i can't explain why ....

    baray98

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.