Mega Search
23.2 Million


Sign Up

Make a donation  
std::ostringstream problem  
News Group: borland.public.cppbuilder.language.cpp

Hi all,

I am using an external frame grabber which uses DLL and internal
structures. To access this DLL, I have to use a strcuture with a member
postr defined as std::ostringstream *postr. Before calling my function I

use this line :

MyStruct.postr = new std::ostringstream;

which returns in a NULL pointer and the functions off the DLL crashes.

Any idea why the constructor std::ostringstream returns me a NULL
pointer ?

Many thanks in advance.

Chritophe




Vote for best question.
Score: 0  # Vote:  0
Date Posted: 10-Jan-2008, at 1:36 PM EST
From: Christophe Minetti
 
Re: std::ostringstream problem  
News Group: borland.public.cppbuilder.language.cpp
Alan Bellingham  writes:

> "Chris Uzdavinis (TeamB)"  wrote:
>
>>Just to be careful about the description, you never call a
>>constructor.  You either declare objects (such as those on the stack)
>>or you evaluate a "new expression", which is calling the "new"
>>operation, which INDIRECTLY may invoke the constructor.
>
> Agreeing with your main thrust here, what's your term for what's
> happening here?
>
>   der::der(int i, int j)
>   : base(i)
>   , member(j)
>   {
>   }


12.6.2 has a good name for those:  base and member initializers.

To me, it's not a matter of finding a good name, but of making sure
people aren't confused by what's actually going on.  For example,
saying that user code is "calling" a constructor shows a slightly
skewed view of how things work.  

It may get across the idea well enough that others know what is meant,
but it still isn't technically correct.  

-- 
Chris (TeamB);

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 11-Jan-2008, at 11:48 AM EST
From: Chris Uzdavinis (TeamB)
 
Re: std::ostringstream problem  
News Group: borland.public.cppbuilder.language.cpp
"Chris Uzdavinis (TeamB)"  wrote:

>Just to be careful about the description, you never call a
>constructor.  You either declare objects (such as those on the stack)
>or you evaluate a "new expression", which is calling the "new"
>operation, which INDIRECTLY may invoke the constructor.

Agreeing with your main thrust here, what's your term for what's
happening here?

  der::der(int i, int j)
  : base(i)
  , member(j)
  {
  }

Alan Bellingham
-- 
Team Browns
ACCU Conference 2008: 2-5 April 2008 - Oxford, UK

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 11-Jan-2008, at 2:37 PM EST
From: Alan Bellingham
 
Re: std::ostringstream problem  
News Group: borland.public.cppbuilder.language.cpp
Christophe MINETTI  writes:

> Sorry for the confusion; the pointer was set to NULL before calling the
> constructor. The problem is that after calling the constructor; it remains
> NULL (the function fails).

Just to be careful about the description, you never call a
constructor.  You either declare objects (such as those on the stack)
or you evaluate a "new expression", which is calling the "new"
operation, which INDIRECTLY may invoke the constructor.

However, if new cannot allocate memory, then it won't invoke the
constructor, and if the constructor throws, new will "unallocate" the
memory and allow the exception to escape.

If the new expression is being evaluated, are you *sure* that no
exception is occurring?  Otherwise, I just don't see how this could
happen.  Try stepping into it, all the way (including the iostream
code), until you get to the line after the "new" expression.  Just to
see what's going on.

Also, evaluate the code in a try-catch block for debugging purposes:

  try {
    MyStruct.postr = new std::ostringstream;
  } 
  catch(...) 
  {
    bool this_is_a_good_place_for_a_breakpoint = true;
  }
  
It may help you detect if an exception really is occurring.


I still think it's one of a few cases: 1) your new expression isn't
really being evaluated at all, 2) you have a new-handler installed
that returns null when memory fails (very unlikely), 3) you are
returning via exception and not noticing it, or 4) you are assigning
to MyStruct.postr, but when you check for null, you're looking at a
DIFFERENT OBJECT named MyStruct, and see that the other one is still
null.

I'm starting to lean toward idea #4 above.  Are you sure that you
don't have any local objects hiding the MyStruct you think you're
initializing? 

-- 
Chris (TeamB);

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 11-Jan-2008, at 9:19 AM EST
From: Chris Uzdavinis (TeamB)
 
Re: std::ostringstream problem  
News Group: borland.public.cppbuilder.language.cpp
Hi and thanks for your answer. After testing the constructor in a separate
program, it works perfectly. It seems that I have problems somewhere else ....

Many thansk anyway

Christophe

Darko Miletic wrote:

> Christophe MINETTI wrote:
> > Hi all,
> >
> > I am using an external frame grabber which uses DLL and internal
> > structures. To access this DLL, I have to use a strcuture with a member
> > postr defined as std::ostringstream *postr. Before calling my function I
> >
> > use this line :
> >
> > MyStruct.postr = new std::ostringstream;
> >
>
> You can not interchange STL objects just like that between your app and
> dll. For this to work both dll and your app must use exactly same C and
> C++ RTL in shared mode - meaning in general that they must be built with
> the same compiler and both use dynamic RTL.
>
> > which returns in a NULL pointer and the functions off the DLL crashes.
> >
> > Any idea why the constructor std::ostringstream returns me a NULL
> > pointer ?
>
> I do not think this is possible.
>
> Can you post stripped down sample that replicates the problem?


Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 11-Jan-2008, at 1:34 PM EST
From: Christophe Minetti
 
Re: std::ostringstream problem  
News Group: borland.public.cppbuilder.language.cpp
Sorry for the confusion; the pointer was set to NULL before calling the
constructor. The problem is that after calling the constructor; it remains
NULL (the function fails).

Christophe

"Chris Uzdavinis (TeamB)" wrote:

> Christophe MINETTI  writes:
>
> > MyStruct.postr = new std::ostringstream;
> >
> > which returns in a NULL pointer and the functions off the DLL crashes.
>
> That's really hard to believe.
>
> > Any idea why the constructor std::ostringstream returns me a NULL
> > pointer ?
>
> Constructors do not and *CANNOT* return values.  The only way for new
> to return null is for you to be using a truly ancient compiler, where
> memory allocations fail.  However, since the mid-90's (ratified in
> 1998), the standard required new to throw std::bad_alloc when memory
> could not be allocated.
>
> So either you're using a user-defined overload of operator new which
> does return nulls, or you're using the std::nothrow version (but it
> doesn't look like it from your code), or you have installed a customer
> "new_handler" function which returns null.
>
> However, my *guess* is the solution occam's razor would suggest: your
> program is not actually executing the code that initializes
> MyStruct.postr with the new expression and it remains uninitialized,
> just so happening to be null.  Is that possible?
>
> --
> Chris (TeamB);


Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 11-Jan-2008, at 8:04 AM EST
From: Christophe Minetti
 
Re: std::ostringstream problem  
News Group: borland.public.cppbuilder.language.cpp
Christophe MINETTI wrote:
> Hi all,
> 
> I am using an external frame grabber which uses DLL and internal
> structures. To access this DLL, I have to use a strcuture with a member
> postr defined as std::ostringstream *postr. Before calling my function I
> 
> use this line :
> 
> MyStruct.postr = new std::ostringstream;
> 

You can not interchange STL objects just like that between your app and 
dll. For this to work both dll and your app must use exactly same C and 
C++ RTL in shared mode - meaning in general that they must be built with 
the same compiler and both use dynamic RTL.

> which returns in a NULL pointer and the functions off the DLL crashes.
> 
> Any idea why the constructor std::ostringstream returns me a NULL
> pointer ?

I do not think this is possible.


Can you post stripped down sample that replicates the problem?


Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Jan-2008, at 6:40 PM EST
From: Darko Miletic
 
Re: std::ostringstream problem  
News Group: borland.public.cppbuilder.language.cpp
Christophe MINETTI  writes:

> MyStruct.postr = new std::ostringstream;
>
> which returns in a NULL pointer and the functions off the DLL crashes.

That's really hard to believe.

> Any idea why the constructor std::ostringstream returns me a NULL
> pointer ?

Constructors do not and *CANNOT* return values.  The only way for new
to return null is for you to be using a truly ancient compiler, where
memory allocations fail.  However, since the mid-90's (ratified in
1998), the standard required new to throw std::bad_alloc when memory
could not be allocated.

So either you're using a user-defined overload of operator new which
does return nulls, or you're using the std::nothrow version (but it
doesn't look like it from your code), or you have installed a customer
"new_handler" function which returns null.

However, my *guess* is the solution occam's razor would suggest: your
program is not actually executing the code that initializes
MyStruct.postr with the new expression and it remains uninitialized,
just so happening to be null.  Is that possible?

-- 
Chris (TeamB);

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Jan-2008, at 11:00 AM EST
From: Chris Uzdavinis (TeamB)