View Full Version : freeing my memory (life should be lived forwards)
baray98
25th September 2007, 16:31
I allocated a buffer in my app like below:
char* ptr = new char[variable]; // its has a variable size but this is known before calling this line
then i pass that pointer to my class
myClass* c = new myClass(ptr);
in my myClass constructor and destructor
myClass::myClass(char* ptr)
{
m_ptr = ptr; //m_ptr is a private char* pointer
}
myClass::~myClass
{
delete [] m_ptr;
}
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
wysota
25th September 2007, 16:38
Not here. The above code is fine.
baray98
25th September 2007, 17:07
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
char* ptr = new char[4];
char* ptr1 = ptr;
//now if I delete ptr
delete[] ptr;
//is ptr 1 still available or is the buffer allocated by new char[4] gone?
//will this produce an exeption
char c = *ptr1
baray98
marcel
25th September 2007, 17:09
ptr1 will point now to a freed memory location.
So you shouldn't delete here too. Once is enough.
baray98
25th September 2007, 20:35
another puzzle but i think its related:
when i allocated the buffer i used
char* ptr = new char [1000];
then,
i pass it to my class like this
myClass* c = new myClass(ptr);
in the destructor of my myClass I did this
myClass::~myClass()
{
int x = sizeof(m_ptr); //remember m_ptr was declared as char*
delete[] m_ptr;
}
int xis equal to four (4) bytes when i stopped (passed) there.
hmmmm....
baray98
wysota
25th September 2007, 20:45
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.
marcel
25th September 2007, 20:46
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.
baray98
25th September 2007, 21:03
here is a snippet of my making the buffer (maybe you guys can figure something out)
//reads frame from valid file
SEECFrame::t_FrameWrapper* firstFrameWrapper = (SEECFrame::t_FrameWrapper*)
new uint8[sizeof(SEECFrame::t_FrameWrapper)];
int frameSize = 0;
file.seek(0);
int filePos = 0;
uint8* m_rawData = NULL;
while (!file.atEnd())
{
//dig frames
filePos = file.pos();
stream.readRawData((char*)firstFrameWrapper, sizeof(SEECFrame::t_FrameWrapper));
frameSize = firstFrameWrapper->lengthL +
(firstFrameWrapper->lengthM << 8) +
(firstFrameWrapper->lengthH<<16) ;
file.seek(filePos);
m_rawData = new uint8 [(int)frameSize]; // where i made the buffer
stream.readRawData((char*)m_rawData, (int)frameSize);
MyFrame* frame = new MyFrame(m_rawData,this); //passed it to the class
if (!frame->isValid())
{
err(tr("Frame No %1 in File : %2 is invalid \n Reason: %3")
.arg(m_VectorFrames.count()+1)
.arg(file.fileName()).arg(frame->errorString()));
delete frame;
delete m_rawData;
break;
}
m_VectorFrames.append(frame);
if (progress.wasCanceled())
{
closeFile();
break;
}
qApp->processEvents();
}
//cleanup firstFrameWrapper
delete[] firstFrameWrapper;
myFrame Class constructor and dtor look like these
myFrame::myFrame( const uint8* rawFrame, QObject * parent )
:QObject(parent),m_Validity(false)
{
//ctor
m_rawFrame = rawFrame;
frameWrapper = (t_FrameWrapper*) m_rawFrame;
m_errorString = QString::null;
//determine which type of records
initRecordType();
processFrame();
}
myFrame::~myFrame()
{
//dtor
if (m_rawFrame != NULL)
{
int x = sizeof(m_rawFrame);
delete[] m_rawFrame;
}
}
this is in may app where i tried to clean up
void UnpackTest::closeFile(void)
{
myFrame* frame = NULL;
while (m_VectorFrames.count())
{
frame = m_VectorFrames.takeLast();
delete frame;
frame = NULL;
}
m_VectorFrames.clear();
gbFrameInfo->clear();
gbSensorInfo->clear();
setWindowTitle("Unpacker");
}
baray98
25th September 2007, 21:59
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
jacek
25th September 2007, 22:22
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.
marcel
25th September 2007, 22:24
When do you delete the elements in m_vectorFrame?
baray98
25th September 2007, 22:37
marcel,
in my closeFile function (this is actually a slot connected to signal closeFile (QAction) triggered()
void UnpackTest::closeFile(void)
{
SEECFrame* frame = NULL;
while (m_VectorFrames.count())
{
frame = m_VectorFrames.takeLast();
delete frame;
frame = NULL;
}
m_VectorFrames.clear();
gbFrameInfo->clear();
gbSensorInfo->clear();
setWindowTitle("Unpacker");
}
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
wysota
25th September 2007, 22:52
It's probably eating a lot elsewhere...
baray98
25th September 2007, 23:33
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
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.