473,416 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Function like "Generic::List<String^>^ buildList(String^ inParm)" and avoiding warning C4172: returning address of local variable or temporary

Hi there,

I have a Managed C++ object (in a DLL) which has a method that is
defined like ...

Generic::List<String^>^ buildList(String^ inParm)

Now, when I compile it, I get "warning C4172: returning address of
local variable or temporary". In good old 'C', that would indicate
that a 'static' was missing from the declaration of the returned value.
How do I avoid this issue in Managed C++?

It does appear to work and fill the array - but I guess the data may
get garbage collected at any point and suddenly disappear.

Any help greatly appreciated.

Cheers,
Paul

(Functions below)
Calling Program
===========
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Configuration;

using XYZ.ABC;

namespace TestDriver
{
class Program
{
static void Main(string[] args)
{
tObject testObject = null;
Console.WriteLine("Start");

testObject = new tObject(7);

IList<StringreturnList = new List<String>();

returnList = testObject.buildList("information");

foreach (String thisValue in returnList)
{
Console.WriteLine("List item: [{0}]", thisValue);
}

Console.WriteLine("Finish");
Console.ReadLine();
}
}
}
AND
Called Object
==========

#include "stdafx.h"
#pragma once

using namespace System;
using namespace System::Globalization;
using namespace System::Collections::Generic;
using namespace System::Collections;
using namespace System::Runtime::InteropServices;

namespace XYZ {
namespace ABC {
public ref class tObject
{
private:
static int item_count = 0;
public:
tObject(int total_items) { item_count = total_items; }
virtual ~tObject() { item_count = 0; }

Generic::List<String^>^ buildList(String^ inParm)
{
int counter;
List<String^ResultList = gcnew List<String^>();

for(counter = 0; counter < item_count; counter++)
{
String^ Item;
Item = gcnew String("list item content " + inParm + " " + (counter
+ 1));
ResultList.Add(Item);
}

return %ResultList;
}
};
}
}

Oct 18 '06 #1
4 2900
rsa_net_newbie wrote:
{
int counter;
List<String^ResultList = gcnew List<String^>();
List<String^>^ ResultList = gcnew List<String^>();
for(counter = 0; counter < item_count; counter++)
{
String^ Item;
Item = gcnew String("list item content " + inParm + " " + (counter
+ 1));
ResultList.Add(Item);
ResultList->Add(Item);
}

return %ResultList;
return ResultList;

You can't return a reference to a temporary object that no longer exists
after the function returns.
}
};
}
}

Tom
Oct 18 '06 #2
Tamas has the exact correct fix. However, since this is "stack semantics"
and not actually memory on the stack, it will work reliably the way it is.
The only thing different is that with stack semantics for an IDisposable
type, the compiler calls Dispose at the end of the scope (like a using
block).
"Tamas Demjen" <td*****@yahoo.comwrote in message
news:eQ**************@TK2MSFTNGP04.phx.gbl...
rsa_net_newbie wrote:
>{
int counter;
List<String^ResultList = gcnew List<String^>();
List<String^>^ ResultList = gcnew List<String^>();
>for(counter = 0; counter < item_count; counter++)
{
String^ Item;
Item = gcnew String("list item content " + inParm + " " + (counter
+ 1));
ResultList.Add(Item);
ResultList->Add(Item);
>}

return %ResultList;
return ResultList;

You can't return a reference to a temporary object that no longer exists
after the function returns.
>}
};
}
}


Tom

Oct 18 '06 #3
Ben Voigt wrote:
Tamas has the exact correct fix. However, since this is "stack semantics"
and not actually memory on the stack, it will work reliably the way it is.
That's a good point. As long as you have a reference to an object, it
probably won't be garbage collected. On the other hand, if the type is
IDisposable, it gets disposed before the function returns.

Hmmmm... I don't really know whether the code in the original post is
perfectly safe or not. Generic::List<Tis not IDisposable, so it won't
be disposed on return that's for sure. I don't know if the compiler
generates code that lets the GC know that that local object is about to
have a life outside of the function. It looks like there's a moment when
the local ResultList handle is not being assigned to any existing
variable, which means it might be ninja GCed. One would have to see the
raw assembly output for that. Well, that warning would be enough to
discourage me anyway.

Tom
Oct 19 '06 #4

Tamas Demjen wrote:
rsa_net_newbie wrote:
{
int counter;
List<String^ResultList = gcnew List<String^>();
List<String^>^ ResultList = gcnew List<String^>();
for(counter = 0; counter < item_count; counter++)
{
String^ Item;
Item = gcnew String("list item content " + inParm + " " + (counter
+ 1));
ResultList.Add(Item);
ResultList->Add(Item);
}

return %ResultList;
return ResultList;

You can't return a reference to a temporary object that no longer exists
after the function returns.
}
};
}
}


Tom
Great - thanks.

Oct 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
9
by: deedub65 | last post by:
I am getting a "Missing ) after argument list" on two different sets of commands in the same script - they are part of Dreamweaver MX's files, so i am not sure how they got altered - thanks for the...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.