Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 5

Qt-interest Archive, August 2007
Boost.Test claims Qt leaks memory


Message 1 in thread

Hi,

I'd appreciate some advice using the Boost unit testing library with
Qt classes.

I'm using MSVC 8.0 on windows XP.

My naive attempts so far suggest that simply creating and destroying a
QObject leaks memory.  At least, that's what boost tells me.  Consider
the following test program:


#define BOOST_AUTO_TEST_MAIN
#include <boost/test/auto_unit_test.hpp>

#include <Qt/qobject.h>

BOOST_AUTO_TEST_CASE( example )
{
    BOOST_CHECK_EQUAL( 1, 1 );

    QObject obj;
}


It builds and runs fine, except that memory leaks are detected; output
is as follows:


Running 1 test case...

*** No errors detected
Detected memory leaks!
Dumping objects ->
{271} normal block at 0x003FC5A0, 20 bytes long.
 Data: <                > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00 
{270} normal block at 0x003FC510, 96 bytes long.
 Data: <$  g  ?      3&g> 24 C9 1A 67 D8 C4 3F 00 00 00 00 00 B4 33 26 67 
{269} normal block at 0x003FC4D8, 8 bytes long.
 Data: <0  g  ? > 30 C9 1A 67 10 C5 3F 00 
{268} normal block at 0x003FC498, 20 bytes long.
 Data: <                > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00 
{267} normal block at 0x003FC438, 48 bytes long.
 Data: <      ?         > 01 00 00 00 D8 C4 3F 00 00 CD CD CD 00 00 00 00 
Object dump complete.


If I comment out the "QObject obj" line, no leaks are detected.  So I
presume Qt is at fault, here.  What's being leaked?  Is there a way to
clean it up?

Thanks,
-Steve

--
 [ signature omitted ] 

Message 2 in thread

On 30 aug 2007, at 21.12, Steve M. Robbins wrote:

> Hi,
>
> I'd appreciate some advice using the Boost unit testing library with
> Qt classes.
>
> I'm using MSVC 8.0 on windows XP.
>
> My naive attempts so far suggest that simply creating and destroying a
> QObject leaks memory.  At least, that's what boost tells me.  Consider
> the following test program:
>
>
> #define BOOST_AUTO_TEST_MAIN
> #include <boost/test/auto_unit_test.hpp>
>
> #include <Qt/qobject.h>
>
> BOOST_AUTO_TEST_CASE( example )
> {
>     BOOST_CHECK_EQUAL( 1, 1 );
>
>     QObject obj;
> }
>
>
> It builds and runs fine, except that memory leaks are detected; output
> is as follows:
>
>
> Running 1 test case...
>
> *** No errors detected
> Detected memory leaks!
> Dumping objects ->
> {271} normal block at 0x003FC5A0, 20 bytes long.
>  Data: <                > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00  
> 00 00
> {270} normal block at 0x003FC510, 96 bytes long.
>  Data: <$  g  ?      3&g> 24 C9 1A 67 D8 C4 3F 00 00 00 00 00 B4 33  
> 26 67
> {269} normal block at 0x003FC4D8, 8 bytes long.
>  Data: <0  g  ? > 30 C9 1A 67 10 C5 3F 00
> {268} normal block at 0x003FC498, 20 bytes long.
>  Data: <                > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00  
> 00 00
> {267} normal block at 0x003FC438, 48 bytes long.
>  Data: <      ?         > 01 00 00 00 D8 C4 3F 00 00 CD CD CD 00 00  
> 00 00
> Object dump complete.
>
>
> If I comment out the "QObject obj" line, no leaks are detected.  So I
> presume Qt is at fault, here.  What's being leaked?  Is there a way to
> clean it up?
>
> Thanks,
> -Steve
>
	I've browsed the source code. There appears to be some static object  
pointers,
that are allocated when a QObject-derived class is first allocated.
	Such objects won't be deallocated until the heap is thrown into the  
bitbucket
on process termination.
	To check if you have a leak or not:
> #define BOOST_AUTO_TEST_MAIN
> #include <boost/test/auto_unit_test.hpp>
>
> #include <Qt/qobject.h>
>
> BOOST_AUTO_TEST_CASE( example )
> {
>     BOOST_CHECK_EQUAL( 1, 1 );
>
>     QObject obj;
	QObject obj2;  // If there are a true leak, the unit test will now  
show twice the amount of leaked memory.
> }
>

------------------------------------------------------
"Home is not where you are born, but where your heart finds peace" -
Tommy Nordgren, "The dying old crone"
tommy.nordgren@xxxxxxxxx


--
 [ signature omitted ] 

Message 3 in thread

On Thu, Aug 30, 2007 at 10:24:36PM +0200, Tommy Nordgren wrote:

> 	I've browsed the source code. There appears to be some static object 
> pointers,
> that are allocated when a QObject-derived class is first allocated.
> 	Such objects won't be deallocated until the heap is thrown into the 
> bitbucket on process termination.

> 	To check if you have a leak or not:
>> #define BOOST_AUTO_TEST_MAIN
>> #include <boost/test/auto_unit_test.hpp>
>>
>> #include <Qt/qobject.h>
>>
>> BOOST_AUTO_TEST_CASE( example )
>> {
>>     BOOST_CHECK_EQUAL( 1, 1 );
>>
>>     QObject obj;
> 	QObject obj2;  // If there are a true leak, the unit test will now show 
> twice the amount of leaked memory.


Right.  It's not a leak in that sense: I ran your modified version and
the amount of "leaked" memory did not change.

The issue for me is to be able to clean up after Qt so that the
Boost.Test leak detection shows only true leaks in my code.  If
I always get a report about these static objects, I'll never see
the true leaks amongst the noise.

Cheers,
-Steve

Attachment:

Attachment: signature.asc
Description: Digital signature


Message 4 in thread

On 8/30/07, Steve M. Robbins <steve@xxxxxxxxx> wrote:

> Right.  It's not a leak in that sense: I ran your modified version and
> the amount of "leaked" memory did not change.
>
> The issue for me is to be able to clean up after Qt so that the
> Boost.Test leak detection shows only true leaks in my code.  If
> I always get a report about these static objects, I'll never see
> the true leaks amongst the noise.

You could give "Visual Leak Detector" a try:

http://www.codeproject.com/tools/visualleakdetector.asp

Jens

-- 
 [ signature omitted ] 

Message 5 in thread

On 30 aug 2007, at 23.37, Steve M. Robbins wrote:

> On Thu, Aug 30, 2007 at 10:24:36PM +0200, Tommy Nordgren wrote:
>
>> 	I've browsed the source code. There appears to be some static object
>> pointers,
>> that are allocated when a QObject-derived class is first allocated.
>> 	Such objects won't be deallocated until the heap is thrown into the
>> bitbucket on process termination.
>
>> 	To check if you have a leak or not:
>>> #define BOOST_AUTO_TEST_MAIN
>>> #include <boost/test/auto_unit_test.hpp>
>>>
>>> #include <Qt/qobject.h>
>>>
>>> BOOST_AUTO_TEST_CASE( example )
>>> {
>>>     BOOST_CHECK_EQUAL( 1, 1 );
>>>
>>>     QObject obj;
>> 	QObject obj2;  // If there are a true leak, the unit test will  
>> now show
>> twice the amount of leaked memory.
>
>
> Right.  It's not a leak in that sense: I ran your modified version and
> the amount of "leaked" memory did not change.
>
> The issue for me is to be able to clean up after Qt so that the
> Boost.Test leak detection shows only true leaks in my code.  If
> I always get a report about these static objects, I'll never see
> the true leaks amongst the noise.
>
> Cheers,
> -Steve

Try the following (at global scope, in the file implementing main)
class myAlloc {
public:
	myAlloc() { allocate(); };
	~myAlloc() {};
	static void allocate()
	{
		QObject obj;
	};
};

static myAlloc all;

This will allocate and destroy  a QObject instance before main is run.
I think this will prevent the static pointers allocated by QObject from
being recorded by the Boost unit tests.
------
What is a woman that you forsake her, and the hearth fire and the  
home acre,
to go with the old grey Widow Maker.  --Kipling, harp song of the  
Dane women
Tommy Nordgren
tommy.nordgren@xxxxxxxxx



--
 [ signature omitted ] 

Message 6 in thread

On Fri, Aug 31, 2007 at 10:55:50AM +0200, Tommy Nordgren wrote:
>
> On 30 aug 2007, at 23.37, Steve M. Robbins wrote:

>> The issue for me is to be able to clean up after Qt so that the
>> Boost.Test leak detection shows only true leaks in my code.  If
>> I always get a report about these static objects, I'll never see
>> the true leaks amongst the noise.

> Try the following (at global scope, in the file implementing main)
> class myAlloc {
> public:
> 	myAlloc() { allocate(); };
> 	~myAlloc() {};
> 	static void allocate()
> 	{
> 		QObject obj;
> 	};
> };
>
> static myAlloc all;
>
> This will allocate and destroy  a QObject instance before main is run.
> I think this will prevent the static pointers allocated by QObject from
> being recorded by the Boost unit tests.

Clever!  I hadn't thought of trying to allocate stuff before the
leak detector starts.

Unfortunately, this version doesn't work: the same "leaks" are
detected.  I don't know the precise details of the boost leak
detection, but I suspect it is also statically initialized and you
can't guarantee the order of static initializers.

You said yesterday browsing the code showed some static objects being
initialized.  I had a quick look yesterday and didn't spot them.  Do
you recall what file or class was doing the allocation?

Thanks,
-Steve

Attachment:

Attachment: signature.asc
Description: Digital signature