qt - c++ constructor malfunctioning -


I'm trying to program a QT widget application that relates to the linked list of nodes, 3 characters in the node * Data is a member and 2 integer members and a "next" indicator of type "node", my problem is saving the list of * links * in the list of four * members in the third letter * as the member, I use the debugger Have tried and found that its length is all 3 i.e. Titles, lappubs, lend pub are started for the same price, and all 3 four * members are getting the same value, the node constructor is as follows

  node :: node (char * Titl) , Char * auth, char * pub, int prime, int stockp) {int lentitl, lenauth, lenpub; Lenpub = strlen (pub); Lentitl = strlen (titl); Lenauth = strlen (certification); Title = new four [lentitl + 1]; Author = new four [lanith + 1]; Publisher = new sheet [lenpub + 1]; Strcpy (title, titl); Strcpy (author, certification); Strcpy (publisher, pub); Value = pri; Stockposition = stockp; Next = zero; }  

The node functions are called by the second class function named "AdBook", and the main window of the adbook. If CPP is called, then the function call for the adbook is as follows

  zero MainWindow :: on_addbook_clicked () {char * titl, * auth, * pub; Int p, stockp; Titl = UI- & gt; TITLE->. ToPlainText () toLatin1 () data () .; Certification = UI-> Author->. ToPlainText () toLatin1 () data () .; Pub = UI- & gt; Publisher->. ToPlainText () toLatin1 () data () .; Pri = UI- & gt; Price->. ToPlainText () toInt (); Stockp = UI- & gt; Stockposition- & gt; ToPlainText () toInt () .; P.addbook (titl, certification, pub, pri, stockp); }  

The function call for the node is as follows:

  void shop :: addbook (char * titl, char * auth, char * pub, Int pre, int stockpiece) {node * p = new node (title, body, pub, pre, stockp); If (start == tap) {start = p; End = p; } And {p-> gt; Next = start; = P start; }}  

The entire project is a zip and it is a link to the output

The screen shot of the output is

As you can see in the image, the" publisher "is set to all 3 four * of the string node in the text edit Can explain why this is happening?

  titl = ui- & gt; Title-> ToPlainText () toLatin1 () data () ..  

This is probably the issue: toLatin1 () gives a new QByteArray which is the owner of its data, that QByteArray Specify the internal data pointers of titl from . However, QByteArray is only a temporary variable and will be destroyed in the next line of code. When QByteArray is destroyed, it will clear its data, which means that your titl is now indicating memory which was already free - that is Recommended solution options:

  1. instead of four * QString titl points

    Use / code> in your node class, it is very easy to deal with issues of memory management

  2. Ensure your QByteArray is temporary From you data Required (unless you have strcpy data):

    QByteArray titl = ui-> title-> toPlainText (). ToLatin1 (); QByteArray auth = ui-> author-> toPlainText (). ToLatin1 (); QByteArray pub = ui-> Publisher-> Plain Text (). ToLatin1 (); Pri = UI-> price-> toPlainText () toInt () .; Stockp = UI-> stockposition-> toPlainText () toInt () .; P.addbook (titl.data), auth.data (), pub.data (), pre, stockp);


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -