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

Qt-interest Archive, April 2008
Class memory allocation


Message 1 in thread

Hi,

I am new to QT/C++ . I am facing a problem in the way I am allocating the
class. My application crashes. The code is something like this:

class MyData
{
private:
  QString Myname;

public:
  MyData();
  ~MyData();

   void setMyName (const QString& str)  { Myname = str};

}


MyData *setMyData = (MyData*)malloc(sizeof(MyData));
QString tmp1("This is my name");
setMyData->setMyName (tmp1);


At the last line the application crashes. The call flow shows the following:

46 KERNEL32!InterlockedDecrement()  0x7c80979d
45 q_atomic_decrement() ..\..\src\corelib\arch\qatomic_windows.h:218
0x101487c9
44 QBasicAtomic::deref() ..\..\src\corelib\thread\qatomic.h:91 0x1014c269

43 QString::operator=() tools\qstring.cpp:1078 0x1004e455
42 setMyName() src\MyData.h:42 0x0040338a

Instead it works fine if I use :
MyData *setMyData = new MyData();

I am using QT 4.3 on Windows.


Will appreciate any help/comments.

Thanks,
Andy

Message 2 in thread

Hi,
  "Andy Gynn" <andy.gynn@xxxxxxxxx> wrote in message 
  (...)
  Instead it works fine if I use :
  MyData *setMyData = new MyData();

And what is wrong with that solution? I never use the malloc way mayself, just new. For my applications, that works fine. 

André 

Message 3 in thread

I am not clear why using "new" works and malloc doesn't. Can anyone please
comment.

Thanks,
Andy

On Tue, Apr 1, 2008 at 7:54 AM, André Somers <andre@xxxxxxxxxxxxxxxx> wrote:

>  Hi,
>
> "Andy Gynn" <andy.gynn@xxxxxxxxx> wrote in message
> (...)
> Instead it works fine if I use :
> MyData *setMyData = new MyData();
>
> And what is wrong with that solution? I never use the malloc way mayself,
> just new. For my applications, that works fine.
>
> André
>

Message 4 in thread

Hi,

Andy Gynn wrote:
> I am not clear why using "new" works and malloc doesn't. Can anyone 
> please comment.

malloc just allocates memory; new also calls the class' constructor. 
Likewise with free/delete.

Tim
----------------------------------------------------------------------
dr. t. dewhirst                                [t] +44 (0)1738 450 465
director                                       [w] www.bugless.co.uk
bugless software development ltd.

[a] algo business centre, glenearn road, perth, PH2 0NJ

--
 [ signature omitted ] 

Message 5 in thread

Accidentally sent it only to Andy before, sorry for the double messages but
I thought it would be nice if that information is available in the list
archive

2008/4/1, Andy Gynn <andy.gynn@xxxxxxxxx>:
>
> I am not clear why using "new" works and malloc doesn't. Can anyone please
> comment.
>
> Thanks,
> Andy


new calls constructors of all class objects, malloc doesn't. That leaves
your QString member variable filled with garbage. When you try to assign
something to it and the assignment function tries to access any of the
garbage-filled QString internals it crashes.
-- 
 [ signature omitted ] 

Message 6 in thread

Thanks....this clarifies

On Tue, Apr 1, 2008 at 8:12 AM, Kalle Last <kalle.last@xxxxxxxxx> wrote:

> Accidentally sent it only to Andy before, sorry for the double messages
> but I thought it would be nice if that information is available in the list
> archive
>
> 2008/4/1, Andy Gynn <andy.gynn@xxxxxxxxx>:
> >
> > I am not clear why using "new" works and malloc doesn't. Can anyone
> > please comment.
> >
> > Thanks,
> > Andy
>
>
> new calls constructors of all class objects, malloc doesn't. That leaves
> your QString member variable filled with garbage. When you try to assign
> something to it and the assignment function tries to access any of the
> garbage-filled QString internals it crashes.
> --
> Kalle Last
>

Message 7 in thread

Andy Gynn wrote:
>class MyData
>{
>private:
>  QString Myname;
>
>public:
>  MyData();
>  ~MyData();
>
>   void setMyName (const QString& str)  { Myname = str};
>
>}
>
>
>MyData *setMyData = (MyData*)malloc(sizeof(MyData));
>QString tmp1("This is my name");
>setMyData->setMyName (tmp1);
>
>
>At the last line the application crashes.

That comes as no surprise. The code above is syntactically correct, but 
completely invalid in C++.

You forgot to call the MyData constructor before calling any function in 
the object. And the only way to call the constructor is via "new".

If you *really* need to allocate your own memory, you have two options:
1) create the function MyData::operator new(size_t)

2) use the placement operator new, like this:
	MyData *setMyData = (MyData*)malloc(sizeof(MyData));
	new (setMyData) MyData;

Needless to say that solution #2 is much harder to understand. Few C++ 
developers know about the placement new operator.
-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 8 in thread

Thiago Macieira wrote:
> 
> 2) use the placement operator new, like this:
> 	MyData *setMyData = (MyData*)malloc(sizeof(MyData));
> 	new (setMyData) MyData;

If you decide to use placement new, don't forget to call destructor 
explicitly before free();

-- 
 [ signature omitted ] 

Message 9 in thread

Am Dienstag, 1. April 2008 schrieb Andy Gynn:
> I am new to QT/C++ . I am facing a problem in the way I am allocating the
> class. My application crashes. The code is something like this:
> MyData *setMyData = (MyData*)malloc(sizeof(MyData));
> QString tmp1("This is my name");
> setMyData->setMyName (tmp1);
<snip>
> Instead it works fine if I use :
> MyData *setMyData = new MyData();

So why don't you use the C++-way of creating new objects?
(The right way of thinking is not to allocate memory for some object, but to 
create a new object of a given type.)
That has the advantage that it is not only a slice of memory big enough to fit 
your object into, but the memory also gets initialized and destructed 
correctly with new and delete by calling constructor and destructor 
automatically.

Have fun,

Arnold
-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 10 in thread

> Hi,
> 
> I am new to QT/C++ . I am facing a problem in the way I am allocating 
> the class. My application crashes. The code is something like this:
> 
> class MyData
> {
> private:
>   QString Myname;
> 
> public:
>   MyData();
>   ~MyData();
> 
>    void setMyName (const QString& str)  { Myname = str};    
> 
> }
> 
> 
> MyData *setMyData = (MyData*)malloc(sizeof(MyData));
> QString tmp1("This is my name");
> setMyData->setMyName (tmp1);
> 
> 
> At the last line the application crashes. The call flow shows the following:
> 
> 46 KERNEL32!InterlockedDecrement()  0x7c80979d   
> 45 q_atomic_decrement() ..\..\src\corelib\arch\qatomic_windows.h:218 
> 0x101487c9   
> 44 QBasicAtomic::deref() ..\..\src\corelib\thread\qatomic.h:91 
> 0x1014c269   
> 43 QString::operator=() tools\qstring.cpp:1078 0x1004e455
> 42 setMyName() src\MyData.h:42 0x0040338a   
> 
> Instead it works fine if I use :
> MyData *setMyData = new MyData();
> 
> I am using QT 4.3 on Windows.
> 
> 
> Will appreciate any help/comments.
When you use malloc, only space is allocated. When you use new, the 
class constructor is called.

> 
> Thanks,
> Andy   

--
 [ signature omitted ]