About preventing memory leaks
-
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
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
-
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
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
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?
-
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?
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
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
-
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
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
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.
-
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.
Newbie00 wrote:
What does it mean?
The same this as
sizeof(var)
andreturn(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
-
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
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
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.