Skip to content
This repository has been archived by the owner on Apr 17, 2022. It is now read-only.

memory errors in 376c8c65fcc33eb31541a971e6c8b7e9c2013f18 #3821

Closed
wzdev-ci opened this issue Dec 6, 2012 · 18 comments
Closed

memory errors in 376c8c65fcc33eb31541a971e6c8b7e9c2013f18 #3821

wzdev-ci opened this issue Dec 6, 2012 · 18 comments

Comments

@wzdev-ci
Copy link
Contributor

wzdev-ci commented Dec 6, 2012

resolution_fixed type_bug | by Reg312


I'm using VS2010 for compiling warzone for my debug experiments
Source file stats.cpp was changed in latest commints in master-version (converting stats to ini)
since this changes i cannot run warzone, getting memory errors!

Also i found way to fix this memory erros
i just need add strdup() calls to places where QT string get converted to c-string
e.g.
weaponName = ini.value("turret").toString().toUtf8().constData();
weaponName = strdup(ini.value("turret").toString().toUtf8().constData());

after this changes i can run warzone.


Issue migrated from trac:3821 at 2022-04-16 10:44:31 -0700

@wzdev-ci
Copy link
Contributor Author

wzdev-ci commented Dec 6, 2012

vexed changed priority from normal to blocker

@wzdev-ci
Copy link
Contributor Author

wzdev-ci commented Dec 6, 2012

vexed changed blocking which not transferred by tractive

@wzdev-ci
Copy link
Contributor Author

wzdev-ci commented Dec 6, 2012

vexed changed blockedby which not transferred by tractive

@wzdev-ci
Copy link
Contributor Author

wzdev-ci commented Dec 6, 2012

vexed changed title from memory errors in Warzone compiled in VS2010 (related to new ini-stats and QT) to memory errors in 376c8c65fcc33eb31541a971e6c8b7e9c2013f18

@wzdev-ci
Copy link
Contributor Author

wzdev-ci commented Dec 6, 2012

vexed commented


This has nothing to do with VS2010.
This is a case of not understanding what is being done behind the scenes, and what you should and shouldn't do when using Qsettings.

I'll do a quick fix shortly.

@wzdev-ci
Copy link
Contributor Author

wzdev-ci commented Dec 6, 2012

vexed uploaded file 0003-Don-t-assign-memory-that-has-been-previously-freed-y.patch (9.1 KiB)

@wzdev-ci
Copy link
Contributor Author

wzdev-ci commented Dec 6, 2012

vexed changed _comment0 which not transferred by tractive

@wzdev-ci
Copy link
Contributor Author

wzdev-ci commented Dec 6, 2012

vexed commented


Should work for all platforms.
Test please.

Reg312, you would leak memory by doing that.

You can't just assign a byte array to a const char since the destructor is
executed before the assignment.
So you end up assigning memory that was freed.

refs #3794
refs #3795 (I have no idea which is what and where this patch came from)

Broken in:
Revision: 376c8c65fcc33eb31541a971e6c8b7e9c2013f18
Author: felippico

@wzdev-ci
Copy link
Contributor Author

wzdev-ci commented Dec 6, 2012

Reg312 commented


vexed, i know about memory leak in my solution, i just dont understand some things to find better way
e.g. i dont see where is destructor called for byteArray in your patch?
looks like QT should care about this.

Tested patch: it works. stats was loaded without errors.

@wzdev-ci
Copy link
Contributor Author

wzdev-ci commented Dec 6, 2012

NoQ commented


Emm, but it's a stack variable. The destructor is called automatically when the function returns, right?

@wzdev-ci
Copy link
Contributor Author

wzdev-ci commented Dec 7, 2012

Reg312 commented


2NoQ: may be i created QByteArray-variable and put it to some list, in that case i dont want destructor called.
as far i understand stack variable is just pointer to memory allocated by QByteArray class.
so i see following possibilites
case 1: QT uses garbage collector
case 2: QT automatically call destructor for such variables on function return. But how it determines more permanent stuff?
case 3: we need call destructor for objects we have created
...i hope someone can clarify :)

@wzdev-ci
Copy link
Contributor Author

wzdev-ci commented Dec 7, 2012

Per commented


string.toUtf8().constData() gives you a temporary c-string reference that must be used immediately.

QByteArray is allocated on the stack, and when it leaves scope it will call its own destructor, that's why there is no memory leaks with vexed's patch. However, I'd rather we used QString than QByteArray here.

I should have caught this error during review. Sorry. Will commit fix.

@wzdev-ci
Copy link
Contributor Author

wzdev-ci commented Dec 7, 2012

Per uploaded file fixmemorybug.diff (24.3 KiB)

Another fix based on QString, also fixes some more memory leaks.

@wzdev-ci
Copy link
Contributor Author

wzdev-ci commented Dec 7, 2012

vexed commented


It compiles and it don't crash, and looks saner.

@wzdev-ci
Copy link
Contributor Author

wzdev-ci commented Dec 8, 2012

Reg312 commented


tested fixmemorybug.diff​ - ok, compiles and properly load stats

*just noticed oil derrick have 100$ price in master

@wzdev-ci
Copy link
Contributor Author

wzdev-ci commented Dec 8, 2012

Per changed status from new to closed

@wzdev-ci
Copy link
Contributor Author

wzdev-ci commented Dec 8, 2012

Per changed resolution from `` to fixed

@wzdev-ci
Copy link
Contributor Author

wzdev-ci commented Dec 8, 2012

Per commented


Fixed in 97fe0b62b9ab206275c403be19bc483cbc8cdb98

@wzdev-ci wzdev-ci closed this as completed Dec 8, 2012
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant