Results 1 to 3 of 3

Thread: Different instances of my class uses the same buffer????

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Different instances of my class uses the same buffer????

    I have this class , I show you the constructor:

    Qt Code:
    1. W_stream::W_stream{
    2. char mybuffer [265536];
    3. the_stream = new stringstream ;
    4. the_stream->rdbuf()->pubsetbuf(mybuffer,265536);
    5. }
    To copy to clipboard, switch view to plain text mode 
    This class has put and get methods to write and read data and has a int loc; to tell me the localization of internal pointer.

    Ok, I create 2 instances:
    my_wstream1= new W_stream();
    my_wstream2= new W_stream();

    And I use my_wstream1.put(a short int) 234 times and my_wstream2.put(a short int) 745.

    Later my_wstream1 has loc of 234*4 and 745*4. Up to here all is right.

    But later my_wstream1.get gives me values that it be supossed has been written by my_wstream2 ????

    The question is mybuffer are the same ?? I dont understand ???
    Mybuffer is private to W_stream.(also "the_stream")
    Any idea ?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Different instances of my class uses the same buffer????

    Show us the actual code. If this is (almost) your real code then the behaviour is correct -- you are overwriting your own stack. If you write enough bytes, you'll crash your app
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Different instances of my class uses the same buffer????

    If this is how the constructor really looks like, then "Mybuffer is private to W_stream."" is not true. You are creating new variable mybuff, which is different from this->mybuff. This new variable is destroyed when constructor exits, so the_stream keeps invalid buffer pointer. Remove the "char mybuff[]" declaration from constructor.
    Another thing:
    Qt Code:
    1. the_stream->rdbuf()->pubsetbuf(mybuffer,265536);
    To copy to clipboard, switch view to plain text mode 
    use sizeof() instead of hard-coded numbers:
    Qt Code:
    1. the_stream->rdbuf()->pubsetbuf(mybuffer,sizeof(mybuff)/sizeof(char));
    To copy to clipboard, switch view to plain text mode 
    Its easier to correct the number in one place.

Similar Threads

  1. Replies: 2
    Last Post: 2nd April 2011, 14:35
  2. how to run two instances of Qt simulator in windows xp.
    By hasnain in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 15th September 2010, 08:31
  3. Chaining QIODevice Instances
    By wswartzendruber in forum Newbie
    Replies: 9
    Last Post: 29th July 2009, 20:23
  4. QFtp in a separate instances
    By baray98 in forum Qt Programming
    Replies: 9
    Last Post: 11th April 2008, 05:54
  5. accessing my own class-instances
    By mikro in forum Newbie
    Replies: 3
    Last Post: 11th July 2006, 00:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.