5 Replies Latest reply on Jul 30, 2009 6:12 AM by riza.guntur

    How to destroy a stream object

    riza.guntur

      I got memory access violation when I use the destructor method. Is there any way to destroy a stream explicitly?

        • How to destroy a stream object
          Ceq

          Stack objects are automatically destroyed at the end of their scope:

          float fun1(float *v_data, float *size ) {
              s_data1 = Stream<float > (2, size );
              s_data1.load(v_data );
              {
                  Stream<float> s_temp(2, size );
                  ...
              } // dispose s_temp
              ...
              return val;
          } // dispose s_data1

           

          Heap objects are destroyed calling delete (only once) or automatically at the end of the program.

          float fun2(float *v_data, float *size ) {
              Stream<float> *s_data_ptr = new Stream<float > (2, size );
              s_data_ptr-> load(v_data );
              ...
              delete s_data_ptr;  // dispose s_data_ptr
              return val;
          }

           

          If you still get memory access violation I can paste a full example here, but probably it's related to memory corruption or pointer handling.

            • How to destroy a stream object
              riza.guntur

              Is there no other way? I mean out of scope isn't too convenient or it it the only way ATM

                • How to destroy a stream object
                  riza.guntur

                  Help anybody. Is there no other way?

                    • How to destroy a stream object
                      Raistmer
                      Originally posted by: riza.guntur

                      Help anybody. Is there no other way?


                      Cause Stream<> is C++ templated object - no other ways AFAIK.
                      But, it's pretty easy to limit area of existance of object even not using heap allocations for it.
                      you could just place additional brakets.
                      For example:
                      //code here....
                      {//here I need stream object, and only here
                      int s=10;
                      Stream< int > abc(1,&s);
                      //here I use abc
                      }//and here abc destroyed, its destructor will called.
                      //other code here.

                      EDIT: actually Ceq already listed that case too :)
                        • How to destroy a stream object
                          riza.guntur

                          Thank you very much.

                          When I read Ceq for a glance I didn't understand, I didn't see and didn't understand about stack and heap objects.

                          Anyway my problem has been finished using heap object as return value (or returning pointer, or whatever the type) with stack object destroyed at the end of scope such as one below.

                          Stream<float4> *input_to_fuzzy(float2 *input_array, unsigned int rank, unsigned int *input_stream_size, unsigned int *fuzzy_stream_size)
                          {
                              Stream<float2> input(rank, input_stream_size);//stream input training
                              Stream<float4> *fuzzy_number = new Stream<float4>(rank, fuzzy_stream_size);//x mean, y for max, z for min
                              input.read(input_array);
                              max_min_mean(input,*fuzzy_number);
                              return fuzzy_number;
                          }